Linux多线程练习1
来源:互联网 发布:caffe windows教程 编辑:程序博客网 时间:2024/06/08 07:49
Linux多线程练习1
#include<stdio.h>#include<pthread.h>#include<unistd.h>#include<stdlib.h>#include<string.h>void *thread_function(void *arg);char message[]="Hello World!";void main(){ pthread_t thread; int res; void *thread_result; res=pthread_create(&thread,NULL,thread_function,(void *)message); if(res!=0) { perror("thread creation failed\n"); exit(EXIT_FAILURE); } printf("waiting for thread to finish..\n"); res=pthread_join(thread,&thread_result); if(res!=0) { perror("Threa join failed\n"); exit(EXIT_FAILURE); } printf("thread joined,it returned %s\n",(char *)thread_result); printf("message is now %s\n",message); exit(EXIT_FAILURE);}void *thread_function(void *arg){ printf("thread function is running,Argument was %s\n",(char *)arg); sleep(3); strcpy(message,"Bye!"); pthread_exit("Thank you for the CPU time");}
首先,我们定义了在创建线程时需要由它调用的一个函数的原型。如下所示:
void *thread_function(void *arg)
根据pthread_create的要求,它只有一个指向void的指针作为参数,返回的也是指向void的指针。稍后,我们将介绍这个函数的定义(及其实现)。
在main函数中,我们首先定义了几个变量,然后调用pthread_create开始运行新线程。如下所示:
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread,NULL,thread_function,(void *)message);
我们向pthread_create函数传递了一个pthread_t类型对象的地址,今后可以用它来引用这个新线程。我们不想改变默认的线程的属性,所以设置第二个参数为NULL。最后两个参数分别为将要调用的函数和一个传递给该函数的参数。
如果这个调用成功了,就会有两个线程在运行。原先的线程(main)继续执行pthread_create后面的代码,而新线程开始执行thread_function函数。
原先的线程在查明新线程已经启动后,将调用pthread_join函数,如下所示:
res = pthread_join(a_thread,&thread_result);
我们给该函数传递两个参数,一个是正在等待其结束的线程的标识符,另一个是指向线程返回值的指针。这个函数将等到它所指定的线程终止后才返回。然后主线程将打印新线程的返回值和全局变量message的值,最后退出。
新线程在thread_function函数中开始执行,它先打印出自己的参数,休眠一会儿,然后更新全局变量,最后退出并向主线程返回一个字符串。新线程修改了数组message,而原先的线程也可以访问该数组。如果我们调用的是fork而不是pthread_create,就不会有这样的效果。
#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<pthread.h>#include<string.h>int run_now=1;int count=0;void *thread1_function(void *arg);void *thread2_function(void *arg);int main(){ pthread_t thread1,thread2; int res1,res2; void *thread1_result; void *thread2_result; printf("main thread is starting \n"); res1=pthread_create(&thread1,NULL,thread1_function,NULL); if(res1!=0) { perror("create thread1 failed!\n"); exit(EXIT_FAILURE); } res2=pthread_create(&thread2,NULL,thread2_function,NULL); if(res2!=0) { perror("thread2 create failed\n"); exit(EXIT_FAILURE); } printf("waiting for thread to finish \n"); res1=pthread_join(thread1,&thread1_result); printf("thread1 exit returned %s\n",(char *)thread1_result); res2=pthread_join(thread2,&thread2_result); printf("thread2 exit returned %s\n",(char *)thread2_result); return 0;}void *thread1_function(void *arg){ while(count++<20) { if(run_now==1) { printf("in thread1 count is %d\n",count); run_now=2; } else { sleep(3); } } pthread_exit("thread1 exit");}void *thread2_function(void *arg){ while(count++<20) { if(run_now==2) { printf("in thread2 count is %d\n",count); run_now=1; } else { sleep(3); } } pthread_exit("thread2 exit");}
每个线程通过设置run_now变量的方法来通知另一个线程开始运行,然后,它会等待另一个线程改变了这个变量的值后再次运行。这个例子显示了两个线程之间自动地交替执行,同时也再次阐明了一个观点,即这两个线程共享run_now变量。
好,这篇就介绍这这两个吧,下篇我们继续!
- Linux多线程练习1
- Linux编程练习 --多线程1--线程创建
- Linux编程练习 --多线程1--线程创建
- Linux编程练习 --多线程1--线程创建
- Linux多线程练习2
- linux 多线程练习、面试题
- Linux基础练习_多线程
- 多线程练习1
- java多线程练习1
- Linux编程练习 --多线程3--mutex
- Linux编程练习 --多线程4--条件变量
- Linux编程练习 --多线程5--信号量(semaphore)
- Linux编程练习 --多线程3--mutex
- Linux编程练习 --多线程4--条件变量
- Linux编程练习 --多线程5--信号量(semaphore)
- linux c 多线程编程练习(一)
- linux c 多线程编程练习(二)
- linux c 多线程编程练习(三)
- 使用到的工具
- 《NLP汉语自然语言处理原理与实践》第四章 NLP中的概率图模型
- 西瓜书第一章 假设空间和版本空间
- git查看当前分支所有提交者及其次数
- 欢迎使用CSDN-markdown编辑器
- Linux多线程练习1
- uva 10041 Vito's Family
- 多路复用I/O的优缺点
- Hadoop集群环境下HBase集群slave节点总是掉问题
- Ubuntu终端常见几种字体颜色表示的意义
- jhipster与cas整合简介
- Linux 下搭建iscsi共享存储
- 聊一聊用户体验
- SQL SERVER中强制类型转换cast和convert的区别