Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <semaphore.h>
- // Costanti di comodo
- #define FALSE 0
- #define TRUE 1
- pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
- sem_t count_sem;
- int count, n;
- void init_vars (){
- // La coda è inizialmente vuota
- // Inizializzo i semafori
- count = 0;
- n = 3;
- sem_init (&count_sem, 0, 0);
- }
- void* thread_function_count (void* arg){
- printf("Prima istruzione th: %s", my_id);
- pthread_mutex_lock (&count_mutex);
- count +=1;
- sem_post(&count_sem);
- pthread_mutex_unlock (&count_mutex);
- while(count != n){
- sem_wait(&count_sem);
- }
- printf("Seconda istruzione th: %s", my_id);
- return NULL;
- }
- int main(int argc, char* argv[]){
- pthread_t T1_thread_id;
- char* T1_name = "t#1";
- pthread_t T2_thread_id;
- char* T2_name = "t#2";
- pthread_t T3_thread_id;
- char* T3_name = "t#3";
- init_vars();
- // Faccio partire i thread
- pthread_create(&T1_thread_id,NULL,&thread_function_count,(void*)T1_name);
- pthread_create(&T2_thread_id,NULL,&thread_function_count,(void*)T2_name);
- pthread_create(&T3_thread_id,NULL,&thread_function_count,(void*)T3_name);
- pthread_join(T1_thread_id,NULL);
- pthread_join(T2_thread_id,NULL);
- pthread_join(T3_thread_id,NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement