专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C语言

linux C死锁有关问题

发布时间:2011-06-28 16:16:05 文章来源:www.iduyao.cn 采编人员:星星草
linux C死锁问题
6 int t = 0;
  7 pthread_mutex_t mutex;
  8
  9 void my_lock(pthread_mutex_t *mutex)
 10 {
 11 pthread_mutex_lock(&mutex,NULL);
 12 }
 13 
 14 void my_unlock(pthread_mutex_t *mutex)
 15 {
 16 pthread_mutex_unlock(&mutex);
 17 }
 18 
 19 void * thread_func(void *arg)
 20 {
 21 // pthread_mutex_lock(&mutex);
 22 my_lock(&mutex); //把这个注释掉换成上面的就不死锁
 23 int *i = (int *)&arg;
 24 sleep(1);
 25 printf("t=%d,i=%d----",t,*i);
 26 int num = t + *i;
 27 t++;
 28 printf("this is test:%d\n",num);
 29 // pthread_mutex_unlock(&mutex);
 30 my_unlock(&mutex); //把这个注释掉换成上面的就不死锁
 31 char cmd[1024] = "diff -w /home/node104/jzhe/he/code/1.txt /home/node104/jzhe/he/code/2.txt";
 32 system(cmd);
 33 return 0;
 34 }
 35 
 36 int main()
 37 {
 38 pthread_mutex_init(&mutex,NULL);
 39 pid_t thread[10];
 40 int i = 0 ;
 41 int j = 0;
 42 int thread_num = 5;
 43 for(i=0; i<thread_num; i++)
 44 {
 45 pid_t pid;
 46 pthread_create(&thread[i],NULL,thread_func,(void *)i);
 47 }
 48 for(i=0; i<thread_num; i++)
 49 {
 50 pthread_join(thread[i],NULL);
 51 }
 52 }

------解决方案--------------------
C/C++ code
void my_lock(pthread_mutex_t *mutex)
{
pthread_mutex_lock(mutex); // 去掉&
}
 
void my_unlock(pthread_mutex_t *mutex)
{
pthread_mutex_unlock(mutex); // 去掉&
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: