pthread_cond_signal只能喚醒已經處於pthread_cond_wait的線程


    也就是說,如果signal的時候沒有線程在condition wait,那么本次signal就沒有效果,后續的線程進入condition wait之后,無法被之前的signal喚醒。
    測試代碼:   

 1 #include <stdio.h>
2 #include <pthread.h>
3 #include <unistd.h>
4
5 pthread_cond_t cond;
6 pthread_mutex_t mutex;
7
8 void *test_thread(void *arg)
9 {
10 printf("Signal main thread...\n");
11 pthread_cond_signal(&cond);
12 return NULL;
13 }
14
15 int main()
16 {
17 pthread_t thread;
18
19 pthread_cond_init(&cond, NULL);
20 pthread_mutex_init(&mutex, NULL);
21
22 pthread_create(&thread, NULL, test_thread, NULL);
23
24 sleep(5);
25 pthread_mutex_lock(&mutex);
26 pthread_cond_wait(&cond, &mutex);
27
28 printf("Main thread signaled, quit...\n");
29 return 0;
30 }

http://www.cnblogs.com/super119/archive/2011/07/29/2120761.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM