1

このプロデューサー/コンシューマー プログラムで pthread_join を使用しようとしていますが、セグメンテーション エラーが発生し続けます。私の目的は、すべてのプロデューサー スレッドが終了するのを待ってから、すべてのコンシューマーを終了することです。しかし、最初のスレッドが参加した後、セグメンテーション違反が発生します。Web を検索しましたが、有用なものは見つかりませんでした。何か案は?

using namespace std ;
int main()
{
  pthread_mutex_init(&mutex , NULL);
 sem_init(&full , 0 ,0);
 sem_init(&empty , 0 , BUFFER_SIZE);
  pthread_t ProducerThread , ConsumerThread;

 int *aa = new int [4];
 for(int i = 0 ; i < 4 ; i++)
 {
  aa[i] = i;
  pthread_t t;
  int k= pthread_create(&t , NULL, Produce , &aa[i]);
  if(k!=0)
  cout<<"Errot"<<endl;
  else  
  printf("Creating Producer %d \n", i);
 }
 int *bb = new int[2];
 for(int i = 0 ; i < 2 ; i++)
 {
  bb[i] = i;
  pthread_t t;
  pthread_create(&t , NULL, Consume , &bb[i]);
  printf("Creating Consumer %d \n", i);
 }
int s;
  for (int i=0;i<4;i++){
  s = pthread_join(aa[i],NULL);
               if (s != 0)
                   cout<< "pthread_join error" ;}
4

1 に答える 1