Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <stdio.h>
- //gcc main.c -o monProgramme -lpthread
- void* thread2 (void* arg);
- void* thread1 (void* arg);
- int main (void)
- {
- int val=0;
- pthread_t monthread1;
- pthread_t monthread2;
- pthread_create (&monthread1, NULL, thread1, &val);
- pthread_create (&monthread2, NULL, thread2, &val); /* Création des threads */
- pthread_join (monthread1, NULL);
- pthread_join (monthread2, NULL); /* Attente de la fin des threads */
- printf ("val =%d\n",val);
- return 0;
- }
- void* thread1 (void* arg)
- {
- int *val = arg;
- printf ("thread1 ON \n");
- for (int i=0;i<100;i++){
- (*val)++;
- }
- printf ("thread1 OFF \n");
- pthread_exit(NULL); /* Fin du thread */
- }
- void* thread2 (void* arg)
- {
- int *val = arg;
- printf ("thread2 ON \n");
- for (int i=0;i<100;i++){
- (*val)++;
- }
- printf ("thread2 OFF \n");
- pthread_exit(NULL); /* Fin du thread */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement