Newsgroups: comp.programming.threads
From: Alexander Terekhov <terek...@web.de>
Date: Thu, 13 Sep 2001 10:01:32 +0200
Local: Thurs, Sep 13 2001 4:01 am
Subject: Re: pthread condition variable deadlock?
Andy Barclay wrote: you do not need volatiles, two mutexes and > I am writing some course material on multi-threading using posix > The problem is that the program deadlocks. On a single processor, it > I've been programming long enough that I'm 99% sure its a bug in my > Anyway, if anyone can help me, it would be appreciated. > /* solution to producer/consumer problem */ > #define BUFFERSIZE 5 > int buffer[BUFFERSIZE]; > void *producer(void *tmp) > void *consumer(void *tmp) > int main() > /* initialize the buffer to 0 */ > /* initialize the condition variables and mutexes*/ > pthread_create(?prodtid,NULL,producer,NULL); > pthread_join(prodtid,NULL); double signaling (notempty¬full) in both producer and consumer routines.. /* solution to producer/consumer problem */ pthread_mutex_t mutex; #define BUFFERSIZE 5 int buffer[BUFFERSIZE]; void* producer(void* name) } void* consumer(void* name) { while(1) { pthread_mutex_lock(&mutex); while (nitems == 0 && !finish) pthread_cond_wait(¬empty,&mutex); if (finish && nitems == 0) break; printf("%d %s %s\n",buffer[consumed], buffer2[consumed], (const char*)name); consumed=(consumed+1)%BUFFERSIZE; nitems--; pthread_mutex_unlock(&mutex); pthread_cond_signal(¬full); } pthread_mutex_unlock(&mutex); return NULL; } int main() { pthread_t prodtid[2],constid[3]; int i; /* initialize the buffer to 0 */ /* initialize mutex and condition variables */ pthread_create(prodtid+0,NULL,producer,"PROD-1"); Sleep( 1000 ); // 1 sec pthread_mutex_lock(&mutex); pthread_cond_broadcast(¬full); pthread_join(prodtid[0],NULL); pthread_cond_destroy(¬empty); return 0; } regards, alexander. You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||