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

看板LinuxDev作者 (B'z)時間17年前 (2008/04/15 16:39), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串2/3 (看更多)
我把程式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: 140.116.39.134

04/15 18:28, , 1F
把scanf丟到lock外面,thread內用另一個變數去接scanf的值
04/15 18:28, 1F

04/15 18:29, , 2F
然後在 lock 內去指定 value 的值為輸入的值
04/15 18:29, 2F

04/15 18:53, , 3F
謝謝樓上~我知道怎麼用了
04/15 18:53, 3F
文章代碼(AID): #1816dAiQ (LinuxDev)
文章代碼(AID): #1816dAiQ (LinuxDev)