a09 Subj : help with basic multithreading in C on solaris To : comp.programming.threads From : Winbatch Date : Sat Feb 26 2005 03:14 pm Hi, I'm trying to learn multithreading and it doesn't seem to be working for me. I have a feeling it has to do with the fact that I'm writing to files rather than to printf, but maybe not. Basically, I wanted to see if it would be faster to write to 4 files at the same time (parallel) rather than 4 in a row (serially). however, when my multithreaded code executes, it seems to do them in order anyway (I expected to see Starting/Ending all mixed rather than in order). Does anyone know what I'm doing wrong? (Note, I also tried to do it with 4 different functions (ie, go, go1, go2, etc.., but that didn't seem to fix it). See code below and output below. #include "stdio.h" #include /* pthread functions and data structures */ void * go ( void * data1 ) { long i=0; FILE * file; char * name; name = (char *) data1; printf( "Starting '%s'\n", name ); file = fopen( name, "w" ); for ( i=0;i< 10000000;i++) { fprintf( file, "%ld\n", i ); } fclose(file); printf( "Ending '%s'\n", name ); pthread_exit(0); } int main() { int thr_id, thr_two, thr_three, thr_four; /* thread ID for the newly created thread */ pthread_t p_thread, thread2, thread3,thread4; /* thread's structure */ char a1[]="a1"; char b1[]="b1"; char c1[]="c1"; char d1[]="d1"; printf( "Creating threads...\n" ); thr_id = pthread_create(&p_thread, NULL, go, (void*)&a1); printf( "Creating thread 2. ..\n" ); thr_two = pthread_create(&thread2, NULL, go, (void*)&b1); printf( "Creating thread 3. ..\n" ); thr_three = pthread_create(&thread3, NULL, go, (void*)&c1); printf( "Creating thread 4. ..\n" ); thr_four = pthread_create(&thread4, NULL, go, (void*)&d1); pthread_join(p_thread, 0); pthread_join(thread2, 0); pthread_join(thread3, 0); pthread_join(thread4, 0); return 0; } cc -mt MultiTest.c -o MultiTest /export/CUST/systems/dan/process/development/MultiThread> ./MultiTest Creating threads... Creating thread 2. .. Creating thread 3. .. Creating thread 4. .. Starting 'a1' Ending 'a1' Starting 'b1' Ending 'b1' Starting 'c1' Ending 'c1' Starting 'd1' Ending 'd1' . 0