From: Sefer T. <se...@ho...> - 2003-04-08 07:54:51
|
Hi! I've been testing a short threaded program, and I noticed that sleep(x), although utilizes no cpu, it schdules poorly (the program slows down almost to a halt). This is a simple threaded queue handler program, which sleeps 2 sec. between each update to the queue, however, after 2-3 such updates, the sleep seems to triple (or worse) waiting often 10 seconds or more (while being completely idle). This happens to me on Mandrake 9.1 on i686 running valgrind both 1.9.4 and a recent snapshot from the CVS. This might indicate some problem in scheduling blocking commands in general (or sleep in particular), after all, why should a sleep(2) wait for 10 seconds (while the cpu is entirely idle). If anyone has any ideas or can offer an explanation, I'd appreciate your comments. Thanks, Sefer. #### Makefile all: gcc -g -Wall -D_REENTRANT -o go *.c -lpthread #### test.c #include <pthread.h> #include <unistd.h> #include <stdio.h> #include <string.h> #define MAXT 4 #define MAXD 10 typedef struct { pthread_mutex_t my_mutex; pthread_cond_t my_cond; int count; } data_t; static data_t gdata[MAXT]; static void data_add(data_t *data) { pthread_mutex_lock(&data->my_mutex); while ( data->count >= MAXD ) { pthread_cond_wait(&data->my_cond, &data->my_mutex); } if ( !data->count ) pthread_cond_signal(&data->my_cond); ++data->count; pthread_mutex_unlock(&data->my_mutex); } static void data_remove(data_t *data) { pthread_mutex_lock(&data->my_mutex); while ( data->count == 0 ) { pthread_cond_wait(&data->my_cond, &data->my_mutex); } if ( data->count >= MAXD ) pthread_cond_signal(&data->my_cond); --data->count; pthread_mutex_unlock(&data->my_mutex); } static void *thread_proc(void *ptr) { printf("Thread created!\n"); for ( ; ; ) { data_remove((data_t *) ptr); printf("Got some data.\n"); } return NULL; } static void run() { int i, rc; pthread_t tid; for ( i = 0; i < MAXT; ++i ) { pthread_mutex_init(&gdata[i].my_mutex, NULL); pthread_cond_init(&gdata[i].my_cond, NULL); gdata[i].count = 0; rc = pthread_create(&tid, NULL, thread_proc, gdata + i); } for ( ; ; ) { data_add(gdata + 0); data_add(gdata + 1); sleep(2); } } int main() { run(); return 0; } _________________________________________________________________ Tired of spam? Get advanced junk mail protection with MSN 8. http://join.msn.com/?page=features/junkmail |