diff options
author | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-06-11 18:50:47 +0200 |
---|---|---|
committer | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-06-11 18:50:47 +0200 |
commit | 7ecbcce58aa7a33915a150ad3f48924c1158779d (patch) | |
tree | 021a8b56587ba44ab779ee5d1e647eb0232b3568 /04_exercise/threadpool.c | |
parent | ddb7d43143ff664948f27cd2cd4e18bdfcce6118 (diff) | |
download | betriebssysteme-7ecbcce58aa7a33915a150ad3f48924c1158779d.tar.gz betriebssysteme-7ecbcce58aa7a33915a150ad3f48924c1158779d.zip |
Basic Implementation
Diffstat (limited to '04_exercise/threadpool.c')
-rw-r--r-- | 04_exercise/threadpool.c | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/04_exercise/threadpool.c b/04_exercise/threadpool.c index 510e83e..93b8807 100644 --- a/04_exercise/threadpool.c +++ b/04_exercise/threadpool.c @@ -7,6 +7,7 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <stdatomic.h> #define MAX_FUTURES 2048 typedef struct Thread { @@ -28,7 +29,7 @@ ThreadPool threadPool; /* TODO: interne Hilfsfunktionen hinzufügen */ void * poolWorkerFunc(void * v_index) { size_t index = * (size_t *) v_index; - printf("Thread %zu started", index); + printf("Thread %zu started \n", index); return NULL; } @@ -51,8 +52,7 @@ int tpInit(size_t size) { void tpRelease(void) { for (size_t i = 0; i < threadPool.size; ++i) { if (pthread_cancel(threadPool.threads[i].pthread) != 0) { - perror("Thread doesn't exist anymore"); - exit(-1); + fprintf(stderr, "The thread %zu had already exited. \n", threadPool.threads[i].index); } } for (size_t i = 0; i < threadPool.size; ++i) { @@ -66,6 +66,19 @@ void tpRelease(void) { void tpAsync(Future *future) { alPush(&threadPool.al, (void *) future); + char expected = FUT_WAITING; + if(!atomic_compare_exchange_strong(&future->status, &expected, FUT_IN_PROGRESS )) { + perror("The future has been scheduled before"); + exit(-1); + } + + future->fn(future); + expected = FUT_IN_PROGRESS; + if(!atomic_compare_exchange_strong(&future->status, &expected, FUT_IN_PROGRESS)) { + perror("The future got marked as done by another thread"); + exit(-1); + } + } void tpAwait(Future *future) { |