threads 1 利用变量同步

来源:互联网 发布:淘宝达人头像怎么更换 编辑:程序博客网 时间:2024/05/17 01:21
[root@localhost ch12]# cat test1.c #include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>char message[]="hello";void* thread_function(void *arg);void *thread_result;int main(){int res;pthread_t a_thread;res=pthread_create(&a_thread,NULL,&thread_function,(void*)message);//if(res!=0){perror("pthread_create error");exit(EXIT_FAILURE);}printf("already create a thread\n");int i=0;while(i++<10){printf("main ");usleep(1000*100);//100ms}}void* thread_function(void *arg){int ii=0;while(ii++<10){printf("sub ");usleep(1000*100);}}[root@localhost ch12]# make test1cc -D_REENTRANT  -lpthread  test1.c   -o test1

[root@localhost ch12]# ./test1already create a thread
main sub main sub main sub main sub sub main sub main sub main main sub sub main main sub [root@localhost ch12]# 
当执行test1时,先输出了already create a thread然后过了一段时间
所有的main sub main sub .....一下子都输出了,..有点奇怪哦
修改line26 ,加上\n,如下
printf("main \n");
便正常,main sub 一个接一个按秩序打印出来
[root@localhost ch12]# make test1cc -D_REENTRANT  -lpthread  test1.c   -o test1[root@localhost ch12]# ./test1already create a threadmain sub sub main sub main main sub sub main sub main main sub sub main main sub main sub [root@localhost ch12]# ./test1already create a threadmain sub sub main sub main main sub main sub main sub main sub sub main sub main sub main [root@localhost ch12]# ./test1already create a threadmain sub sub main sub main main sub main sub main sub main sub main sub main sub main sub [root@localhost ch12]# ./test1already create a threadmain sub sub main main sub main sub main sub main sub main sub main sub sub main sub main [root@localhost ch12]# 
从./test1的几次输出,也可以看出两个线程是"交替"执行的,但不是完全的交替,或许在竞争cpu时间片

上面那个奇怪的问题并不是出现在线程身上,而是print和sleep的问题,
如下普通例子
[root@localhost ch12]# cat test3.c #include <stdio.h>int main() {printf("hello1");sleep(2);printf("hello2\n");printf("hello3");sleep(2);}[root@localhost ch12]# gcc -o test3 test3.c
[root@localhost ch12]# ./test3
2 seconds passed then
hello1hello2
2 seconds passed again then
hello3[root@localhost ch12]# 
看来printf是遇到\n才会真正输出(到stdout),他的这个特性遇到sleep的时候便显现出来

并且从上面的输出可以看出是两个线程在竞争cpu时间片
因此输出时序不太确定
如果要两个线程有序输出的话,则需要互相勾结好--
--即商量好你输出一次,我再输出,然后你再输出..美其名可曰线程同步
比如下
[root@localhost ch12]# cat test1.c#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>char message[]="hello";void* thread_function(void *arg);void *thread_result;int run_now=1;int main(){int res;pthread_t a_thread;res=pthread_create(&a_thread,NULL,&thread_function,(void*)message);//if(res!=0){perror("pthread_create error");exit(EXIT_FAILURE);}printf("already create a thread\n");int i=0;while(i++<10){  if (run_now == 1){printf("main \n");            run_now = 2;}else{            usleep(1000*100);}}}void* thread_function(void *arg){int ii=0;while(ii++<10){  if (run_now == 2){printf("sub ");            run_now = 1;}else{            usleep(1000*100);}}}
执行之,有序了,每个线程输出5次
[root@localhost ch12]# make test1cc -D_REENTRANT  -lpthread  test1.c   -o test1[root@localhost ch12]# ./test1already create a threadmain sub main sub main sub main sub main sub [root@localhost ch12]# ./test1already create a threadmain sub main sub main sub main sub main sub [root@localhost ch12]# 
但线程同步的正当途径应该是信号量和互斥量.


原创粉丝点击