3
Exécution en parallèle
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #define NLOOPS 1000
- void *fping( void *np ) {
- int i, j;
- for ( i = 0; i < NLOOPS; i++ ) {
- printf( "ping." );
- for ( j = 0; j < 1000; j++ )
- (void) rand();
- }
- pthread_exit( 0 );
- }
- void *fpong( void *np ) {
- int i, j;
- for ( i = 0; i < NLOOPS; i++ ) {
- printf( "pong." );
- for ( j = 0; j < 1000; j++ )
- (void) rand();
- }
- pthread_exit( 0 );
- }
- int main( int argc, char *argv[] ) {
- pthread_t ping, pong;
- pthread_create( &ping, 0, fping, 0 );
- pthread_create( &pong, 0, fpong, 0 );
- pthread_exit( 0 );
- }
$ gcc -Wall -lpthread pingpong.c -o pingpong
$ ./pingpong
ping.ping.pong.ping.pong.ping.pong.ping.ping.pong.ping.pong.pong...
Commentaires