Pthreads & Signals
Anthony Gabrielson
agabriel at home.tzo.org
Wed Nov 17 10:49:08 EST 2004
Okay,
I think I may be missing something then...
On Wed, 17 Nov 2004, Kevin D. Clark wrote:
>
> 1: errno isn't set by any of the pthread_ functions (at least, none
> that I can think of). So, your code like this is wrong:
Oops - fixed...
>
> 2: You're calling pthread_cond_wait() without first locking the mutex
> that you're using.
Okay I see what your saying. I wanted to use the wait as a condition
before I grabbed the mutex. So in the attached code I send the first
signal after I unlock while the other thread is grabbing and then waiting
for the signal. It still doesn't work... Where did I go wrong?
> 3: You're calling pthread_cond_wait() without enclosing this call in
> a loop. This is nearly always wrong.
Okay its in a while loop now. I figured since its a wait statement and
its not going to return until it has the signal, perhaps I can get away
without it. However, since it could have missed the signal before getting
to the wait that makes sense; my bad.
> Don't take the following statement the wrong way (I'm trying to be
> impassive and clinical here): Just because your code "works" on
> several other platforms doesn't mean that it is correct.
>
> Incorrect code will bite you hard at some point.
That statement is true, I buy that, however I can gauge how I'm doing by
testing it in several places. I can't find an example that does what I'm
trying to do so if it works in three other places and not Linux, then my
first guess would be perhaps (not def, but perhaps) its not my code.
Can you see where I went astray?
Thanks,
Anthony
-------------- next part --------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
pthread_mutex_t guard_revc;
pthread_cond_t got_data;
void *thr(void *);
int lock;
int main( int argc, char *argv[] )
{
pthread_t thread_recv;
int result;
lock = 1;
pthread_cond_init( &got_data, NULL );
pthread_mutex_init( &guard_revc, NULL );
result = pthread_create(&thread_recv, NULL,
thr, NULL );
if ( result != 0 ) {
perror( "pthread_create failed" );
printf( "%s\n",strerror(result));
exit ( 3 );
}
printf("main: wait cond\n");
result = pthread_mutex_lock( &guard_revc );
printf("main: Got Mutex %d\n",result);
while (lock == 1){
result = pthread_cond_wait( &got_data, &guard_revc );
printf("main: got cond %d\n",result);
lock = 0;
}
result = pthread_cond_signal( &got_data );
printf("main: send cond %d\n",result);
result = pthread_mutex_unlock( &guard_revc );
usleep(1);
printf("main: Release Mutex %d\n",result);
pthread_join(thread_recv,NULL);
return 0;
}
void *thr( void *arg ){
int result;
result = pthread_mutex_lock( &guard_revc );
printf("thr: Got Mutex %d\n",result);
usleep(1);
result = pthread_mutex_unlock( &guard_revc );
printf("thr: Release Mutex %d\n",result);
result = pthread_cond_signal( &got_data );
printf("thr: Sent Signal %d\n",result);
usleep(1);
while(lock == 0 ){
result = pthread_cond_wait( &got_data, &guard_revc );
printf("thr: Got Signal %d\n",result);
lock = 1;
}
pthread_exit( NULL );
}
More information about the Discuss
mailing list