From 7ecbcce58aa7a33915a150ad3f48924c1158779d Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Thu, 11 Jun 2020 18:50:47 +0200 Subject: Basic Implementation --- 04_exercise/threadpool.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to '04_exercise/threadpool.c') 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 #include #include +#include #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) { -- cgit v1.2.3-54-g00ecf