From 26d58225f0cb3256bf1c715257d2882271f3ecae Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 1 Dec 2021 23:39:19 +0100 Subject: tsdwl: fix cond --- Year_3/TSDWL/ex_20131219/thread.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'Year_3/TSDWL') diff --git a/Year_3/TSDWL/ex_20131219/thread.c b/Year_3/TSDWL/ex_20131219/thread.c index e46d05b..9a6f70a 100644 --- a/Year_3/TSDWL/ex_20131219/thread.c +++ b/Year_3/TSDWL/ex_20131219/thread.c @@ -5,7 +5,7 @@ int m; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; -pthread_cond_t cond; +pthread_cond_t cond = PTHREAD_COND_INITIALIZER; void* fn_t1(void* arg) @@ -16,14 +16,13 @@ fn_t1(void* arg) pthread_mutex_lock(&mutex); if (m >= 1 && m <= 5) { xrand = (rand() % 10) + 1; - printf("Thread 1: (%d, %d)\n", m, xrand); + printf("Thread 1: (m = %d, xrand = %d)\n", m, xrand); m = xrand; - pthread_cond_broadcast(&cond); - pthread_mutex_unlock(&mutex); + pthread_cond_signal(&cond); } else { - pthread_mutex_unlock(&mutex); pthread_cond_wait(&cond, &mutex); } + pthread_mutex_unlock(&mutex); } pthread_exit(NULL); @@ -38,14 +37,13 @@ fn_t2(void* arg) pthread_mutex_lock(&mutex); if (m >= 6 && m <= 10) { xrand = (rand() % 10) + 1; - printf("Thread 2: (%d, %d)\n", m, xrand); + printf("\t\t\tThread 2: (m = %d, xrand = %d)\n", m, xrand); m = xrand; - pthread_cond_broadcast(&cond); - pthread_mutex_unlock(&mutex); + pthread_cond_signal(&cond); } else { - pthread_mutex_unlock(&mutex); pthread_cond_wait(&cond, &mutex); } + pthread_mutex_unlock(&mutex); } pthread_exit(NULL); -- cgit v1.2.3-18-g5258