【问题标题】:Unexpected behaviour of pthread_cond_broadcastpthread_cond_broadcast 的意外行为
【发布时间】:2019-01-17 16:38:54
【问题描述】:

根据我昨天提出的问题here,我编写了一个小代码示例,该示例启动了一些计数和一些等待线程。 等待线程停止pthread_cond_wait,直到它们收到信号。计数线程完成任务后发送信号。

等待的线程接收到它们的信号,每个线程打印出其给定的唯一 ID。

我希望所有等待的线程同时接收到信号,这样每个线程都可以继续执行程序。然而我注意到,输出并不混乱,事实上它们甚至看起来是相当有序的,就像在 FILO 中一样!

现在有很多地方我可能会出错。

这是我的代码:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define counting_threads 100
#define waiting_threads 100

int count = 0;
int counting_thread_ids[counting_threads];
int waiting_thread_ids[waiting_threads];
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;


void init_ids(){
    for(int i = 0; i < counting_threads; i++)
        counting_thread_ids[i] = 2*i;
    for(int j =0; j < waiting_threads; j++)
        waiting_thread_ids[j] = 2*j+1;  
}

void counting(void *t) 
{
    pthread_mutex_lock(&count_mutex);
    count++;

    if (count == counting_threads) {
    sleep(2);
      printf("inc_count(): count = %d  Threshold reached. Signaling waiting threads. \n", count);
           //~ pthread_cond_signal(&count_threshold_cv);           
           pthread_cond_broadcast(&count_threshold_cv); 
      }
    pthread_mutex_unlock(&count_mutex);
    }

void *waiting(void *t) 
{
  long my_id = (long)t;
  //~ printf("Starting watch_count(): thread %ld\n", my_id);

  pthread_mutex_lock(&count_mutex);

    //~ printf("watch_count(): I start waiting now: %ld \n", my_id);
    pthread_cond_wait(&count_threshold_cv, &count_mutex);
    printf("watch_count(): thread %ld Condition signal received.\n", my_id);
    pthread_mutex_unlock(&count_mutex);
    pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
    init_ids(); 

    pthread_t wt[waiting_threads];
    pthread_t ct[counting_threads];  

  /* Initialize mutex and condition variable objects */
  pthread_mutex_init(&count_mutex, NULL);
  pthread_cond_init (&count_threshold_cv, NULL);

    for(int i = 0; i < waiting_threads; i++)
        pthread_create(&wt[i], NULL, waiting, (void*) waiting_thread_ids[i] );
    for(int i = 0; i < counting_threads; i++)
        pthread_create(&ct[i], NULL, counting, (void*) counting_thread_ids[i] );


  /* Wait for all threads to complete */
  for (int i=0; i<waiting_threads; i++) {
    pthread_join(wt[i], NULL);
  }
  for (int i=0; i<counting_threads; i++) {
    pthread_join(ct[i], NULL);
  }


  /* Clean up and exit */
  pthread_mutex_destroy(&count_mutex);
  pthread_cond_destroy(&count_threshold_cv);
  pthread_exit(NULL);

}

【问题讨论】:

  • 您应该阅读pthread_cond_wait() 的手册页。

标签: c multithreading pthreads timing pthread-barriers


【解决方案1】:

pthread_cond_signal() 调用至少解除了在指定条件变量 cond 上阻塞的线程之一(如果有任何线程在 cond 上阻塞)。

pthread_cond_broadcast() 调用会解除当前在指定条件变量 cond 上阻塞的所有线程。

如果多个线程在一个条件变量上被阻塞,调度策略决定线程被解除阻塞的顺序

有关调度策略的更多信息可以找到here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 2016-07-16
    • 2016-05-10
    • 2020-07-23
    • 2021-08-23
    • 2021-11-16
    • 2017-10-20
    • 2016-06-29
    相关资源
    最近更新 更多