1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
// Fonction appelée lors de la création de thread
void *print (void * arg)
{
int i;
for (i = 0 ; i < 5 ; i++) {
printf ("Thread %s\n", (char*)arg);
sched_yield(); // rend la main
}
pthread_exit (0);
}
// MAIN
main (int ac, char **av)
{
pthread_t th1, th2;
void *ret;
// création premier thread
if (pthread_create (&th1, NULL, print, " 1 Hello") < 0) {
fprintf (stderr, "pthread_create error for thread 1\n");
exit (1);
}
// création second thread
if (pthread_create (&th2, NULL, print, " 2 World!") < 0) {
fprintf (stderr, "pthread_create error for thread 2\n");
exit (1);
}
(void)pthread_join (th1, &ret);
(void)pthread_join (th2, &ret);
} |
Partager