还是互斥锁的问题
- C/C++ code
#include<pthread.h>#include<stdlib.h>#include<unistd.h>#include<stdio.h>#include<errno.h>pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;int lock_var=0;time_t end_time;int sum;void pthread1(void *arg);void pthread2(void *arg);void pthread3(void *arg);int main(int arg,char *argv[]){ pthread_t id1,id2,id3; pthread_t mon_th_id; int ret; sum=10; end_time=time(NULL)+10; //pthread_mutex_init(&mutex,NULL); ret=pthread_create(&id1,NULL,(void *)pthread1,NULL); if(ret!=0) perror("pthread cread1"); ret=pthread_create(&id2,NULL,(void *)pthread2,NULL); if(ret!=0) perror("pthread cread2"); ret=pthread_create(&id3,NULL,(void *)pthread3,NULL); if(ret!=0) perror("pthread cread3"); pthread_join(id1,NULL); pthread_join(id2,NULL); pthread_join(id3,NULL); exit(0);}void pthread1(void *arg){ int i; printf("come in thread1\n"); while(time(NULL)<end_time) { if(pthread_mutex_lock(&mutex)!=0)//lock { perror("pthread_mutex_lock 55 line"); } else { printf("pthread1:pthread1 lock the variable\n"); for(i=0;i<2;i++) { sleep(2); lock_var++; printf("in thread1 and lock_var=%d\n",lock_var); } } if(pthread_mutex_unlock(&mutex)!=0)//unlock { perror("pthread_mutex_unlock"); } else { printf("pthread1:pthread1 unlock the variable\n"); sleep(1); } }}void pthread2(void *arg){ printf("come in thread2\n"); int nolock=0; int ret; while(time(NULL)<end_time) { ret=pthread_mutex_trylock(&mutex);//try lock if(ret==EBUSY) printf("pthread2:the vaiable is locked by pthread1\n"); else { if(ret!=0) { perror("pthread_mutex_trylock 93 line"); exit(1); } else printf("pthread2:pthread2 got lock.The variable is %d\n",lock_var); if(pthread_mutex_unlock(&mutex)!=0)//unlock { perror("pthread_mutex_unlock"); } else printf("pthread2:pthread2 unlock the variable\n"); } sleep(1); }}void pthread3(void *arg){ printf("come in thread3 \n"); int nolock=0; int ret; while(time(NULL)<end_time) { ret=pthread_mutex_trylock(&mutex); if(ret!=EBUSY) printf("pthread3:variable is locked by pthread1 or 2\n"); else { if(ret!=0) { perror("pthread_mutex_trylock 123 line"); exit(1); } else printf("pthread3:pthread3 got lock.The variable is %d\n",lock_var); if(pthread_mutex_unlock(&mutex)!=0) perror("pthread_mutex_unlock"); else printf("pthread3:pthread2 unlock the variable\n"); } sleep(3); }}
我的机子上的运行结果为
come in thread3
pthread3:variable is locked by pthread1 or 2
come in thread2
pthread2:the vaiable is locked by pthread1
come in thread1
pthread2:the vaiable is locked by pthread1
pthread2:the vaiable is locked by pthread1
pthread_mutex_trylock 123 line: Success
我怎么感觉很奇怪啊 不是应该先进入thread1 吗 如果没有进入thread1枷锁的话,那thread3又怎么会测试得到变量已经被threa1 或threa2 加过锁了?、
很奇怪测试结果怎么是这样的
居然是最后进入thread1的
球高手赐教
[解决办法]
好像是你无法保证那个线程先执行。
可以设定线程优先级
[解决办法]
1. thread3 里为什么写成 if(ret!=EBUSY)
2. come in thread3 在第一行打印是有可能的, 实际上 printf("come in thread1\n"); printf("come in thread2\n");都可能, 因为创建线程后只是创建, 先执行谁不一定
3. pthread2:the vaiable is locked by pthread1
come in thread1
先打印2, 再1也是可能的, 虽然先在1里lock但print的顺序不一定的
正常的语句是顺序执行的, 但2个线程的print就不一定了, 因为print是不可重入的,因为输出的只有一个硬件
楼主可以试试一个 printf, 一个perror也许会按顺序走,因为他们从软件上是输出到2个设备