Subj : Is this valid or not? To : comp.programming.threads From : Randy Howard Date : Tue Feb 15 2005 07:49 am I recently ran into some problems with gcc and pthreads under cygwin. I was trying out a little hello world to see it build, specify the correct compiler flags, libs, etc. This will hang when run on an SMP system, but not a single CPU (or a when bash.exe is pinned to a single processor via task manager). Running it repeatedly, as in: while [ 1 ] do ./hello.exe done .... is a good way to make it fail quickly. Seems to run as expected on other pthread systems, such as SuSE 9.1 pro. I was hoping that somebody could point out the obvious thing I must be doing wrong before I submit it to cygwin as a bug. I've tried using both of these command lines: gcc -O0 -g0 -D_REENTRANT -Wall hello.c -o hello.exe -lpthread gcc -mthreads -O0 -Wall hello.c -o hello.exe Note that if I comment out the call to printf() labeled "bad guy" the lockups go away. <------- snip ---------> #include #include #include #define THREADS 8 pthread_mutex_t io_mutex = PTHREAD_MUTEX_INITIALIZER; void Lock(pthread_mutex_t *mtx); void Unlock(pthread_mutex_t *mtx); void * PrintHello(void *unused_arg) { Lock(&io_mutex); printf("Hello World!\n"); Unlock(&io_mutex); return NULL; } int main(void) { pthread_t threads[THREADS] = {0}; int rc = 0; int t = 0; for (t=0; t < THREADS; t++) { Lock(&io_mutex); printf("Creating thread %d\n", t); /* bad guy */ Unlock(&io_mutex); rc = pthread_create(&threads[t], NULL, PrintHello, NULL); if (rc != 0) { fprintf(stderr, "error from pthread_create(): %d\n", rc); exit(EXIT_FAILURE); } } for (t=0; t < THREADS; t++) { rc = pthread_join(threads[t], NULL); if (rc != 0) { fprintf(stderr, "error from pthread_join(): %d\n", rc); exit(EXIT_FAILURE); } } puts("All threads joined, seeya.\n\n"); return EXIT_SUCCESS; } void Lock(pthread_mutex_t *mtx) { int rc = 0; rc = pthread_mutex_lock(mtx); if (rc != 0) { fprintf(stderr, "locking mutex, error = %d\n", rc); exit(EXIT_FAILURE); } } void Unlock(pthread_mutex_t *mtx) { int rc = 0; rc = pthread_mutex_unlock(mtx); if (rc != 0) { fprintf(stderr, "unlocking mutex, error = %d\n", rc); exit(EXIT_FAILURE); } } <------- snip ---------> -- Randy Howard (2reply remove FOOBAR) "Making it hard to do stupid things often makes it hard to do smart ones too." -- Andrew Koenig .