From owner-freebsd-hackers Thu Jan 15 06:23:25 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id GAA23962 for hackers-outgoing; Thu, 15 Jan 1998 06:23:25 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from oskar.nanoteq.co.za (oskar.nanoteq.co.za [196.37.91.10]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id GAA23733 for ; Thu, 15 Jan 1998 06:21:04 -0800 (PST) (envelope-from jacques@oskar.nanoteq.co.za) Received: (from jacques@localhost) by oskar.nanoteq.co.za (8.8.8/8.8.5) id QAA24608 for freebsd-hackers@freebsd.org; Thu, 15 Jan 1998 16:19:55 +0200 (SAT) Date: Thu, 15 Jan 1998 16:19:55 +0200 (SAT) From: Jacques Fourie Message-Id: <199801151419.QAA24608@oskar.nanoteq.co.za> To: freebsd-hackers@FreeBSD.ORG Subject: Pthreads question Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk Hi I'm running FreeBSD2.2.5-RELEASE and I'm noticing weird behaviour when using the threaded version of libc, libc_r. Included is an example program. When started with parameter 1, the pthread_cond_timedwait() function returns an error with errno=0. When started with parameter 0 everything seems to be working fine. The only difference between the 2 arguments is that in the first case the pthread_cond_timedwait() is not called in the main thread. Any help will be appreciated. Jacques Fourie -------------------Source code----------------------------------------- /* Compiled with gcc -o threadtest threadtest.c -lc_r */ #include #include #include pthread_t th; pthread_t th2; pthread_t th3; pthread_cond_t co; pthread_mutex_t mu; int fin=0; void fg(void) { struct timespec ts; struct timeval tv; pthread_mutex_lock(&mu); while (!fin) { gettimeofday(&tv,NULL); TIMEVAL_TO_TIMESPEC(&tv,&ts); ts.tv_sec += 1; printf("[.]\n"); if (pthread_cond_timedwait(&co,&mu,&ts) == -1) { if (errno != EAGAIN) { printf("timedwait error: %d\n",errno); break; } } } pthread_mutex_unlock(&mu); } void bg(void) { sleep(2); pthread_mutex_lock(&mu); fin=1; pthread_mutex_unlock(&mu); pthread_cond_signal(&co); } int main(int argc,char * argv[]) { if (argc<2) exit(1); pthread_mutex_init(&mu,pthread_mutexattr_default); pthread_cond_init(&co,pthread_condattr_default); pthread_create(&th,pthread_attr_default,(void *)bg,NULL); if (argv[1][0]=='1') pthread_create(&th2,pthread_attr_default,(void *)fg,NULL); else fg(); pthread_join(th,NULL); pthread_detach(&th); pthread_cond_destroy(&co); pthread_mutex_destroy(&mu); return 0; } ---------------------------------Snip-----------------------------------------