作者bzgibson (B'z)
看板LinuxDev
標題Re: [問題] thread裡面使用scanf的問題
時間Tue Apr 15 16:39:37 2008
我把程式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
1F:推 kaichan:把scanf丟到lock外面,thread內用另一個變數去接scanf的值 04/15 18:28
2F:→ kaichan:然後在 lock 內去指定 value 的值為輸入的值 04/15 18:29
3F:→ bzgibson:謝謝樓上~我知道怎麼用了 04/15 18:53