having trouble with basic multithreading
From: Winbatch (winbatch_at_techie.com)
Date: 02/26/05
- Next message: Tim Prince: "Re: GCC linker throws weird message."
- Previous message: DHOLLINGSWORTH2: "Re: Main function not defined"
- Next in thread: Walter Roberson: "Re: having trouble with basic multithreading"
- Reply: Walter Roberson: "Re: having trouble with basic multithreading"
- Reply: Artie Gold: "Re: having trouble with basic multithreading"
- Reply: sipetar_at_inet.hr: "Re: having trouble with basic multithreading"
- Reply: Mark McIntyre: "Re: having trouble with basic multithreading"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 26 Feb 2005 05:49:03 GMT
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.h> /* 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'
- Next message: Tim Prince: "Re: GCC linker throws weird message."
- Previous message: DHOLLINGSWORTH2: "Re: Main function not defined"
- Next in thread: Walter Roberson: "Re: having trouble with basic multithreading"
- Reply: Walter Roberson: "Re: having trouble with basic multithreading"
- Reply: Artie Gold: "Re: having trouble with basic multithreading"
- Reply: sipetar_at_inet.hr: "Re: having trouble with basic multithreading"
- Reply: Mark McIntyre: "Re: having trouble with basic multithreading"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]