home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
GianuzziV
/
SO1
/
HelloThread.c
< prev
next >
Wrap
C/C++ Source or Header
|
2005-04-03
|
1KB
|
49 lines
/********************************************************
* *
* Multi-threaded "Hello World" *
* sono creati 2 thread, uno scrive Hello, l'altro Word *
* *
* gcc -D_REENTRANT HelloThread.c -lpthread *
* *
********************************************************/
#include <pthread.h>
#include <stdio.h>
void* output( void* );
/* mutex variables */
pthread_mutex_t Lock = PTHREAD_MUTEX_INITIALIZER;
int main( void )
{
pthread_t thr1, thr2;
const char* msg1 = "Hello ";
const char* msg2 = "world ";
pthread_create( &thr1, NULL, output, (void*)msg1 );
pthread_create( &thr2, NULL, output, (void*)msg2 );
pthread_join( thr1, NULL );
pthread_join( thr2, NULL );
printf( "\n" );
return 0;
}
void* output( void* msg )
{
int i;
for( i = 0; i < 10; ++i ) {
pthread_mutex_lock( &Lock );
printf( (char*)msg );
pthread_mutex_unlock( &Lock );
}
return NULL;
}