小编Rus*_*uss的帖子

pthread_cond_broadcast问题

在linux 2.6.30中使用pthreads我试图发送一个信号,这将导致多个线程开始执行.广播似乎只能由一个线程接收.我已经尝试了pthread_cond_signal和pthread cond_broadcast,两者似乎都有相同的行为.对于pthread_cond_wait中的互斥锁,我尝试了两种常见的互斥锁和单独的(本地)互斥锁,没有明显区别.


worker_thread(void *p)
{
   // setup stuff here

   printf("Thread %d ready for action \n", p->thread_no);
   pthread_cond_wait(p->cond_var, p->mutex);
   printf("Thread %d off to work \n", p->thread_no);

   // work stuff
}

dispatch_thread(void *p)
{
   // setup stuff

   printf("Wakeup, everyone ");
   pthread_cond_broadcast(p->cond_var);
   printf("everyone should be working \n");

   // more stuff
}

main()
{
   pthread_cond_init(cond_var);

   for (i=0; i!=num_cores; i++) {
      pthread_create(worker_thread...);
   }

   pthread_create(dispatch_thread...);
}
Run Code Online (Sandbox Code Playgroud)

输出:


  Thread 0 ready for action
  Thread 1 ready for action
  Thread 2 ready …
Run Code Online (Sandbox Code Playgroud)

linux pthreads

5
推荐指数
1
解决办法
3007
查看次数

标签 统计

linux ×1

pthreads ×1