pthread_cond_signal只能唤醒已经处于pthread_cond_wait的线程
2011-07-29 11:07
567 查看
也就是说,如果signal的时候没有线程在condition wait,那么本次signal就没有效果,后续的线程进入condition wait之后,无法被之前的signal唤醒。
测试代码:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_cond_t cond;
pthread_mutex_t mutex;
void *test_thread(void *arg)
{
printf("Signal main thread...\n");
pthread_cond_signal(&cond);
return NULL;
}
int main()
{
pthread_t thread;
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
pthread_create(&thread, NULL, test_thread, NULL);
sleep(5);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("Main thread signaled, quit...\n");
return 0;
}
测试代码:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_cond_t cond;
pthread_mutex_t mutex;
void *test_thread(void *arg)
{
printf("Signal main thread...\n");
pthread_cond_signal(&cond);
return NULL;
}
int main()
{
pthread_t thread;
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
pthread_create(&thread, NULL, test_thread, NULL);
sleep(5);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("Main thread signaled, quit...\n");
return 0;
}
相关文章推荐
- pthread_cond_signal只能唤醒已经处于pthread_cond_wait的线程
- pthread_cond_signal只能唤醒已经处于pthread_cond_wait的线程
- 线程间通讯《代码》pthread_cond_wait/signal
- 线程中的条件变量pthread_cond_wait、pthread_cond_signal
- 线程中的条件变量pthread_cond_wait、pthread_cond_signal
- Linux 2. 关于pthread_cond_signal 唤醒两个线程的问题
- 线程中的条件变量pthread_cond_wait、pthread_cond_signal
- [Linux 线程]深入理解pthread_cond_wait、pthread_cond_signal
- 线程条件锁 pthread_cond_wait()/pthread_cond_signal()
- SB了,pthread_cond_wait(),在等待的时候会解锁(阻塞之后,解锁mutex给别的线程),唤醒后加锁,
- 线程同步之条件变量:pthread_cond_signal和pthread_cond_wait
- Linux下c开发 之 线程通信与pthread_cond_wait()的使用
- pthread_cond_wait与pthread_cond_signal
- larbin中线程相关 pthread_cond_wait() 函数
- pthread_cond_signal和pthread_cond_wait简介
- pthread_cond_signal和pthread_cond_wait简介
- 使用pthread_cond_timedwait把一个线程作为一个定时器使用.
- pthread_cond_wait()与pthread_cond_signal()
- pthread_cond_signal和pthread_cond_wait简介
- 条件锁(条件Mutex)pthread_cond_wait、pthread_cond_signal、pthread_cond_broadcast的使用