I am trying to use pthread_join with this producer-consumer program but I keep getting a segmentantion fault. My purpose is to wait for all the producer threads to end and then terminate all the consumers. But after the first thread joins I get a segmentation fault. I searched the web but haven't found anything useful. Any Ideas?
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" ;}