summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/ex_20131219/thread.c
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-12-01 23:39:19 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-12-01 23:39:19 +0100
commit26d58225f0cb3256bf1c715257d2882271f3ecae (patch)
tree7c51ccc598a4ecf540c3e94965944773d572a517 /Year_3/TSDWL/ex_20131219/thread.c
parenteb4161840396b65cb8e018db042eb8ae101b2646 (diff)
tsdwl: fix cond
Diffstat (limited to 'Year_3/TSDWL/ex_20131219/thread.c')
-rw-r--r--Year_3/TSDWL/ex_20131219/thread.c16
1 files changed, 7 insertions, 9 deletions
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);