Multithreading C中使用条件变量的1-生产者N-消费者队列

Multithreading C中使用条件变量的1-生产者N-消费者队列,multithreading,concurrency,posix,mutex,producer-consumer,Multithreading,Concurrency,Posix,Mutex,Producer Consumer,我正在为我的操作系统考试做一个项目。图为我的流程分为: 1个线程生成器,用于在队列中推送消息 n线程使用者,从队列头弹出消息 1个线程收集器,它告诉生产者生成另一组任务 我最初的伪代码设计是这样的(如果你需要真正的代码,我可以用Makefile和所有的头文件制作一个.tar…) 制作人 while(1) { pushTasks(); /* push messages in the queue */ waitCollector(); /* wait on a CV th

我正在为我的操作系统考试做一个项目。图为我的流程分为:

  • 1个线程生成器,用于在队列中推送消息
  • n线程使用者,从队列头弹出消息
  • 1个线程收集器,它告诉生产者生成另一组任务
我最初的伪代码设计是这样的(如果你需要真正的代码,我可以用Makefile和所有的头文件制作一个.tar…)

  • 制作人

    while(1) {
    
        pushTasks();  /* push messages in the queue */
    
        waitCollector(); /* wait on a CV the signal of the collector */
    
        broadcastToWorkers(); /* tells to all workers to start a new elaboration */
    }
    
  • 所有消费者都这样做

    while(1) 
    {
        while(1)
        {
            popTask();
            doTask();
            if(message of end of stream) break;
        }
        signalToCollector(); /* increment a count and signal on a CV */
        waitProducer(); /* wait signal from producer to restart the elaboration */
    }
    
  • 收集器,它必须同步时间(假设每个时间单位都发生在所有任务完成时)

但我在某个地方遇到了僵局。你知道我应该使用多少互斥和条件变量吗?使每个线程执行
pthread\u cond\u signal
pthread\u cond\u wait
的条件是什么?

请提供。如果不查看代码,调试某个地方的死锁几乎是不可能的。此外,您可以搜索现有的SPMC(单生产者多消费者)实现,网络中有许多实现。请提供。如果不查看代码,调试某个地方的死锁几乎是不可能的。此外,您可以搜索现有的SPMC(单生产者多消费者)实现,网络中有许多实现。
while(1) {
    doStuff();
    waitWorkers(); /* each worker increments a count when it's done... */
    signalToProducer(); /* tells the producer to generate new tasks*/
}