c++ - 如何同步线程(消费者/生产者)

标签 c++ pthreads condition-variable

我有以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <queue>
using namespace std;

queue<int> myqueue;

pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;

void *consumer(void*);
void *producer(void*);

#define COUNT_DONE 10
int count = 0;

main()
{
   pthread_t thread1, thread2;

   pthread_create( &thread2, NULL, &consumer, NULL);
   pthread_create( &thread1, NULL, &producer, NULL);

   pthread_join( thread1, NULL);
   pthread_join( thread2, NULL);

   printf("Final count: %d\n",count);

   system("PAUSE");
   return EXIT_SUCCESS;
}

void *consumer(void*)
{
   for(;;)
   {
      // Lock mutex and then wait for signal to relase mutex
      printf("consumer mutex lock \n");
      pthread_mutex_lock( &count_mutex );
      printf("consumer mutex locked\n");

      // Wait while functionCount2() operates on count
      // mutex unlocked if condition varialbe in functionCount2() signaled.
      printf("consumer wait\n");
      pthread_cond_wait( &condition_var, &count_mutex );
      printf("consumer condition woke up\n");
      myqueue.pop();count--;
      printf("Counter value consumer: %d\n",count);

      printf("consumer mutex unlock\n");
      pthread_mutex_unlock( &count_mutex );

      if(count >= COUNT_DONE) return(NULL);
    }
}

void * producer(void*)
{
    for(;;)
    {
       printf("producer mutex lock\n");
       pthread_mutex_lock( &count_mutex );
       printf("producer mutex locked\n");

       if( count < COUNT_DONE)
       {
           myqueue.push(1);
           count++;
           printf("Counter value producer: %d\n",count);
           printf("producer signal\n");
           pthread_cond_signal( &condition_var );
       }

       printf("producer mutex unlock\n");
       pthread_mutex_unlock( &count_mutex );

       if(count >= COUNT_DONE) return(NULL);

       Sleep(5000);
    }

}

当消费者线程首先获取互斥量时,这个示例工作正常。但是当生产者线程最初最初获取互斥锁时,我将在队列中始终有 1 个消费者无法弹出的整数。

我怎样才能让消费者线程在生产者之前首先获得互斥量。

注意:我正在寻找一种比先启动一个线程更好的方法。

谢谢,

最佳答案

我看到的一个问题是您的消费者实际上并没有检查要完成的工作,它只是盲目地从队列中弹出。

我看到的第二个问题是,您在一个中递增计数并在另一个中递减计数,那么您如何达到终止条件?

从 consumer 中取出忍者“count--”,它应该可以工作。不过,您可能希望在消费者内部执行以下操作:

// Wait for producer to do its thing and tell us there is work to do.
while ( myqueue.empty() ) {
    pthread_cond_wait(&condition_var, &count_mutex);
}
// we've been told there's work to do with the queue,
// and we know there's something ON the queue.
// consume the entire queue.
while ( !myqueue.empty() ) {
  myqueue.pop();
}

// treat count as protected by the mutex, so hoist this test into the lock.
bool workDone = (count >= COUNT_DONE);
pthread_mutex_unlock(&count_mutex);

if(workDone)
    return break;

编辑:消费者的首选版本:

bool workDone = false;
while(workDone == false)
{
    // Lock mutex and then wait for signal to relase mutex
    pthread_mutex_lock( &count_mutex );

    // Wait for producer to do its thing and tell us there is work to do.
    while ( myqueue.empty() )
    pthread_cond_wait( &condition_var, &count_mutex );

    // we've been told there's work to do with the queue,
    // and we know there's something ON the queue.
    // consume the entire queue.
    while ( myqueue.empty() == false ) {
        myqueue.pop();
    }

    // count is protected by the lock so check if we're done before we unlock.
    workDone = (count >= COUNT_DONE);
    pthread_mutex_unlock( &count_mutex );

}
return NULL;

关于c++ - 如何同步线程(消费者/生产者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16641143/

相关文章:

c++ - ffmpeg解码器似乎丢帧

c++ - << 的运算符重载需要 const;产生头痛

multithreading - Posix 线程 :Signal a thread that is running in while loop

multithreading - 在等待通知std::condition_variable的过程中执行 “wait callback”

c++ - 我可以在没有超时的情况下检查 std::condition_variable 吗?

C++ 模板参数之间的比较似乎被忽略了

c++ - C++中如何解析 `auto a(b);`?

C:使用来自单独文件的函数

C Pthreads - 父/子进程

C++11 线程 : notify_all() or notify_one() when I only have one?