summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/ex_20191218/thread.c
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-11-23 20:25:33 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-11-23 20:25:33 +0100
commit5985b13797106e1caf3bb195e52e431dfe188512 (patch)
tree4ef4575b897f1f0310a74a1bebc9ebc9d9c97606 /Year_3/TSDWL/ex_20191218/thread.c
parent9120732acc43cf3e2a42152331fca16a84226635 (diff)
tsdwl: add exam
Diffstat (limited to 'Year_3/TSDWL/ex_20191218/thread.c')
-rw-r--r--Year_3/TSDWL/ex_20191218/thread.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/Year_3/TSDWL/ex_20191218/thread.c b/Year_3/TSDWL/ex_20191218/thread.c
new file mode 100644
index 0000000..7d77646
--- /dev/null
+++ b/Year_3/TSDWL/ex_20191218/thread.c
@@ -0,0 +1,72 @@
+#include <pthread.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <time.h>
+#include <math.h>
+
+int x;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t cond;
+
+void*
+t1_fn(void* arg)
+{
+ int m;
+
+ while (1) {
+ usleep(100 * 1000);
+ m = rand() % 11;
+
+ pthread_mutex_lock(&mutex);
+ printf("th1: (x=%d, m=%d):", x, m);
+ if (x == -1) {
+ break;
+ } else if (x == m) {
+ printf("RISPOSTA CORRETTA\n");
+ x = -1;
+ break;
+ } else if (abs(x-m) > 5) {
+ printf("risposta MOLTO sbagliata\n");
+ pthread_mutex_unlock(&mutex);
+ pthread_cond_wait(&cond, &mutex);
+ } else {
+ printf("risposta sbagliata\n");
+ }
+ pthread_mutex_unlock(&mutex);
+ }
+
+ pthread_mutex_unlock(&mutex);
+ pthread_exit(NULL);
+}
+
+void*
+t2_fn(void* arg)
+{
+ while (1) {
+ usleep(300* 1000);
+ pthread_mutex_lock(&mutex);
+ printf("\tthread 2 %d\n", x);
+ if (x == -1) {
+ pthread_mutex_unlock(&mutex);
+ pthread_exit(NULL);
+ }
+ pthread_cond_signal(&cond);
+ pthread_mutex_unlock(&mutex);
+ }
+}
+
+int
+main()
+{
+ pthread_t t1, t2;
+ srand(time(NULL));
+ x = rand() % 11;
+
+ pthread_create(&t1, NULL, t1_fn, NULL);
+ pthread_create(&t2, NULL, t2_fn, NULL);
+ pthread_join(t1, NULL);
+ pthread_join(t2, NULL);
+
+ return 0;
+}