From c8f625564c6608887de0b8c1c879a3a8d7dfef98 Mon Sep 17 00:00:00 2001 From: Julian Weigt Date: Mon, 9 Feb 2026 21:53:03 +0100 Subject: Handle quitting more user friendly and introduce thread handling thread. --- charf.c | 56 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/charf.c b/charf.c index e9daaf3..e77c1aa 100644 --- a/charf.c +++ b/charf.c @@ -486,15 +486,16 @@ void print_latex_records_to_file(EXPTYPE exponents[P], VALUETYPE ratios[K+1][P], } /*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* cont; } 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; int* finished; bool* abort; } Args; /*Goes through all functions indexed by those numbers which equal the thread number mod NUM_THREADS.*/ void* compute_chunk(void* arguments){ Args* args = arguments; - int i = args -> num_thread; + index_t i = 0UL; + i += args -> num_thread; int* domain_current = args -> domain_current; int d = 0; - while(d >= 0){ + while(d >= 0 && !*(args -> abort)){ d = compute(i, args -> exponents, args -> records_ratio, args -> records_index); if(d > *domain_current){ *domain_current = d; @@ -502,11 +503,24 @@ void* compute_chunk(void* arguments){ } i += NUM_THREADS; } - (*(args -> cont))++; - if(*(args -> cont) >= NUM_THREADS) printf("Calculation finished. Press any button to stop.\n"); + (*(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; + +void* handle_threads(void* arguments){ + Thread_handler_args* args = arguments; + /*Wait for each thread to complete.*/ + for (int i = 0; i < NUM_THREADS; i++) { + pthread_join(args->threads[i], NULL); + printf("In main: Thread %d has ended.\n", i); + } + printf("All calculations finished.\n"); + printf(args->message); +} + int main() { EXPTYPE exponents[P]; exponents[0] = int_to_exptype(1); @@ -521,7 +535,8 @@ int main() { index_t records_index[K+1][P]; for(int k=0; k<=K; k++) for(int p=0; p= NUM_THREADS) user_input_cont = false; } else if( 0 == strcmp(buff,"r")) print_records(exponents,records_ratio,records_index); else if( 0 == strcmp(buff,"t")){ getLine("Enter file name:\n", buff, sz); print_latex_records_to_file(exponents,records_ratio,records_index,buff); } - } while(cont < NUM_THREADS); //Stop once all threads have finished. - - /*Wait for each thread to complete.*/ - for (int i = 0; i < NUM_THREADS; i++) { - pthread_join(threads[i], NULL); - printf("In main: Thread %d has ended.\n", i); - } + } while(user_input_cont); - print_records(exponents,records_ratio,records_index); + /*Wait for thread handler thread to complete.*/ + pthread_join(thread_handler, NULL); + printf("In main: Thread handler has finished.\n"); return 0; } -- cgit v1.2.3