Re: [問題] thread裡面使用scanf的問題
很明顯你的兩個線程出現了鎖等待的問題﹐第一個線程如果沒有收入﹐他就一直
佔用著這個鎖﹐第二個線程肯定不能執行了。
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):
LinuxDev 近期熱門文章
PTT數位生活區 即時熱門文章