0

次のコードがあります。

#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 つあります。

どうすればコンシューマー スレッドに、最初にプロデューサーの前にミューテックスを取得させることができますか。

注: あるスレッドを別のスレッドの前に起動するよりも良い方法を探しています。

ありがとう、

4

1 に答える 1

1

私が見た問題の 1 つは、消費者が実際に行う作業をチェックせず、やみくもにキューからポップするだけだということです。

私が目にする 2 番目の問題は、一方では count を増やし、もう一方では減らすということです。

忍者の「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;
于 2013-05-20T01:12:45.107 に答える