Problem with pthread_con
"dada1" <[email protected]> Fri, 12 Sep 2003 09:12:55 +0200
| Newsgroups | gmane.comp.lib.phil |
|---|---|
| Message-ID | <000501c378fd$4dbb5ed0$890010ac@edumazet> |
Hello Ulrich
I have a problem with pthread_cond_signal() (nptl 0.58)
A very simple program can block. It is very rare.
I dont know yet if this is a kernel bug, a processor bug, or a bug from me !
Usually, doing a strace -p one_pid unblocks the program.
(the threads seems blocked to get the condvar.lock)
Tested on a bi athlon, kernel 2.6.0-test5
Thanks
Eric Dumazet
Sample program (without error checks for clarification) :
#include <pthread.h>
#define MAXWORK 8
struct {
pthread_mutex_t lock ;
pthread_cond_t cond ;
unsigned int unload ;
unsigned int load ;
unsigned long miss ;
struct {
int delai ;
} tab[MAXWORK] ;
} U ;
void *worker(void *arg)
{
int delai ;
for (;;) {
pthread_mutex_lock(&U.lock) ;
while (U.load == U.unload) {
pthread_cond_wait(&U.cond, &U.lock) ;
}
delai = U.tab[U.unload%MAXWORK].delai ;
U.unload++ ;
pthread_mutex_unlock(&U.lock) ;
usleep(delai) ; /* some work */
}
}
void post_work()
{
int delai = rand() % 50000 ;
unsigned int nb ;
pthread_mutex_lock(&U.lock) ;
nb = U.load - U.unload ;
if (nb < MAXWORK) {
U.tab[U.load%MAXWORK].delai = delai ;
U.load++ ;
pthread_mutex_unlock(&U.lock) ;/* so that awaken thread doesnt have
to wait for the lock */
pthread_cond_signal(&U.cond) ;
}
else {
U.miss++ ;
pthread_mutex_unlock(&U.lock) ;
}
}
int main(int argc, char *argv[])
{
time_t t0 , t1 ;
pthread_t tid1 ;
pthread_t tid2 ;
pthread_create(&tid1, 0, worker, 0) ;
pthread_create(&tid2, 0, worker, 0) ;
t0 = time(0) ;
for (;;) {
post_work() ;
usleep(rand() % 50000) ;
t1 = time(0) ;
if (t1 != t0) {
t0 = t1 ;
write(1, "*", 1) ;
}
}
}