Re: [問題] thread裡面使用scanf的問題

看板LinuxDev作者 (好想你)時間17年前 (2008/04/20 20:47), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
很明顯你的兩個線程出現了鎖等待的問題﹐第一個線程如果沒有收入﹐他就一直 佔用著這個鎖﹐第二個線程肯定不能執行了。 ※ 引述《bzgibson (B'z)》之銘言: : 我把程式PO出來 : #include <stdio.h> : #include <stdlib.h> : #include <pthread.h> : int value; : void *thread (void *); : int data; : pthread_mutex_t lock; : int main() : { : pthread_t tid; : data=0; : pthread_mutex_init(&lock,NULL); : pthread_create(&tid,NULL,thread,NULL); : while(1) : { : pthread_mutex_lock(&lock); : data++; : printf("data=%d\n",data); : pthread_mutex_unlock(&lock); : printf("error unlock\n"); : } : pthread_join(tid,NULL); : printf("thread_1 stopped\n"); : return 0; : } : void *thread(void *arg) : { : while(1) : { : pthread_mutex_lock(&lock); : printf("please input value="); : scanf("%d",&value); : pthread_mutex_unlock(&lock); : } : printf("thread_2 stopped\n"); : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.237.184.165
文章代碼(AID): #182pjjZG (LinuxDev)
文章代碼(AID): #182pjjZG (LinuxDev)