From 3f84820a2c0be6333c14740052e243efd06b6a9e Mon Sep 17 00:00:00 2001 From: Julian Weigt Date: Tue, 10 Feb 2026 08:46:07 +0100 Subject: Make thread handler more involved in aborting. --- charf.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/charf.c b/charf.c index 0109d6b..cac1138 100644 --- a/charf.c +++ b/charf.c @@ -509,7 +509,7 @@ void print_records_to_file(EXPTYPE exponents[P], VALUETYPE ratios[K+1][P], index } /*information given to each thread*/ -typedef struct { EXPTYPE exponents[P]; VALUETYPE (*records_ratio)[K+1][P]; index_t (*records_index)[K+1][P]; int num_thread; int* domain_current; int* finished; bool* abort; } Args; +typedef struct { EXPTYPE exponents[P]; VALUETYPE (*records_ratio)[K+1][P]; index_t (*records_index)[K+1][P]; int num_thread; int* domain_current; bool* cont; } Args; /*Goes through all functions indexed by those numbers which equal the thread number mod NUM_THREADS.*/ void* compute_chunk(void* arguments){ @@ -518,20 +518,21 @@ void* compute_chunk(void* arguments){ i += args -> num_thread; int* domain_current = args -> domain_current; int d = 0; - while(d >= 0 && !*(args -> abort)){ + /*Only run while index is still within range and while cont flag is not set to false, i.e. program is not aborted.*/ + while(d >= 0 && *(args -> cont)){ d = compute(i, args -> exponents, args -> records_ratio, args -> records_index); + /*For the user to know when the next larger circle starts being considered.*/ if(d > *domain_current){ *domain_current = d; printf("Start considering length: %d\n",d); } i += NUM_THREADS; } - (*(args -> finished))++; printf("Thread %i finished before index %llu.\n",args -> num_thread,i); return NULL; } -typedef struct { pthread_t *threads; char *message; } Thread_handler_args; +typedef struct { pthread_t *threads; char *message; bool *is_computing; } Thread_handler_args; void* handle_threads(void* arguments){ Thread_handler_args* args = arguments; @@ -540,6 +541,7 @@ void* handle_threads(void* arguments){ pthread_join(args->threads[i], NULL); printf("In main: Thread %d has ended.\n", i); } + *(args -> is_computing) = false; printf("All calculations finished.\n\n"); printf(args->message); } @@ -564,8 +566,8 @@ int main() { index_t records_index[K+1][P]; for(int k=0; k<=K; k++) for(int p=0; p