Ajout Thread
This commit is contained in:
45
ANALE-1/EXO4/thread.c
Normal file
45
ANALE-1/EXO4/thread.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
struct structArg {
|
||||
pthread_mutex_t* mutex;
|
||||
};
|
||||
|
||||
void* func1(void* arg){
|
||||
struct structArg* args = arg;
|
||||
pthread_mutex_lock(args->mutex);
|
||||
for(int i =1;i<1500;i++){
|
||||
printf("OK - 1 - %d\n",i);
|
||||
}
|
||||
pthread_mutex_unlock(args->mutex);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
void* func2(void* arg){
|
||||
struct structArg* args = arg;
|
||||
pthread_mutex_lock(args->mutex);
|
||||
for(int i =1;i<1000;i++){
|
||||
printf("OK - 2 - %d\n",i);
|
||||
}
|
||||
pthread_mutex_unlock(args->mutex);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
int main(void){
|
||||
pthread_t p1,p2;
|
||||
pthread_mutex_t mutex;
|
||||
pthread_mutex_init(&mutex,NULL);
|
||||
|
||||
struct structArg args;
|
||||
args.mutex = &mutex;
|
||||
|
||||
|
||||
pthread_create(&p1,NULL,func1,&args);
|
||||
pthread_create(&p2,NULL,func2,&args);
|
||||
pthread_join(p1,NULL);
|
||||
pthread_join(p2,NULL);
|
||||
pthread_mutex_destroy(&mutex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user