summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/ex_20170201/thread.c
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-11-24 21:59:49 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-11-24 21:59:49 +0100
commit7307ddd09c9d139162802b8ce9672a0a3466b4e3 (patch)
tree8810416101750a7d8f970aef6a3d7c3dc07c76d2 /Year_3/TSDWL/ex_20170201/thread.c
parent425966de0eb7a729913329457a1850c54b7404e2 (diff)
tsdwl: add exam
Diffstat (limited to 'Year_3/TSDWL/ex_20170201/thread.c')
-rw-r--r--Year_3/TSDWL/ex_20170201/thread.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/Year_3/TSDWL/ex_20170201/thread.c b/Year_3/TSDWL/ex_20170201/thread.c
new file mode 100644
index 0000000..34ba440
--- /dev/null
+++ b/Year_3/TSDWL/ex_20170201/thread.c
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <time.h>
+#include <string.h>
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+int sample = 50;
+
+void*
+fn(void* arg)
+{
+ char* th = (char*) arg;
+ int new_sample;
+
+ char indent[3] = "";
+ if (strcmp(th, "B") == 0)
+ strcpy(indent, "\t");
+ else if (strcmp(th, "C") == 0)
+ strcpy(indent, "\t\t");
+
+ while (1) {
+ new_sample = (rand() % 81) + 10;
+ pthread_mutex_lock(&mutex);
+
+ printf("%sSono il thread %s: sample valeva %d, adesso vale %d\n", indent, th, sample, new_sample);
+ if (sample != new_sample) {
+ sample = new_sample;
+ pthread_mutex_unlock(&mutex);
+ } else {
+ pthread_mutex_unlock(&mutex);
+ break;
+ }
+
+ }
+
+ printf("%sSono il thread %s, e sto uscendo!\n", indent, th);
+
+ pthread_exit(NULL);
+}
+
+int
+main()
+{
+ pthread_t th[3];
+ srand(time(NULL));
+
+ pthread_create(&th[0], NULL, &fn, (void*) "A");
+ pthread_create(&th[1], NULL, &fn, (void*) "B");
+ pthread_create(&th[2], NULL, &fn, (void*) "C");
+
+ pthread_join(th[0], NULL);
+ pthread_join(th[1], NULL);
+ pthread_join(th[2], NULL);
+
+ printf("Il valore finale di sample e' %d\n", sample);
+
+ return 0;
+}