3
Un simple fil d'exécution
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <errno.h>
- #include <string.h>
- #define NTHREADS 5
- void *hello( void *tnump ) {
- printf( "Hello World #%d!\n", *(int *) tnump );
- pthread_exit( 0 );
- }
- int main( int argc, char *argv[] ) {
- pthread_t threads[NTHREADS];
- int rc, tnum;
- for ( tnum = 0; tnum < NTHREADS; tnum++ ) {
- printf( "Creating thread #%d\n", tnum );
- rc = pthread_create( &threads[tnum], 0, hello, (void *) &tnum );
- if ( rc )
- fprintf( stderr, "%s\n", strerror( errno ) );
- }
- pthread_exit( 0 );
- }
$ gcc -Wall -lpthread hello.c -o hello
$ ./hello
Creating thread #0
Creating thread #1
Hello World #1!
Creating thread #2
Hello World #2!
Creating thread #3
Hello World #3!
Creating thread #4
Hello World #5!
Hello World #4!
Commentaires