Re: [問題] thread裡面使用scanf的問題
我把程式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
04/15 18:28, 1F
→
04/15 18:29, , 2F
04/15 18:29, 2F
→
04/15 18:53, , 3F
04/15 18:53, 3F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 2 之 3 篇):
LinuxDev 近期熱門文章
PTT數位生活區 即時熱門文章