diff options
| author | Julian Weigt <juw@posteo.de> | 2026-02-09 21:53:03 +0100 |
|---|---|---|
| committer | Julian Weigt <juw@posteo.de> | 2026-02-09 21:53:03 +0100 |
| commit | c8f625564c6608887de0b8c1c879a3a8d7dfef98 (patch) | |
| tree | 705e0ae690523c982a13df750b63d04a15fecf48 | |
| parent | 15cb5ccbbee7abb8ce253632bc2c4febdfaedca5 (diff) | |
Handle quitting more user friendly and introduce thread handling thread.
| -rw-r--r-- | charf.c | 56 |
1 files changed, 38 insertions, 18 deletions
@@ -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<P; p++) records_ratio[k][p] = int_to_valuetype(0); int domain_current = 0; - int cont = 0; + int finished = 0; + bool abort = false; /*Start threads that do the computation.*/ for (int i = 0; i < NUM_THREADS; i++) { @@ -531,34 +546,39 @@ int main() { args[i].records_index = &records_index; args[i].num_thread = i; args[i].domain_current = &domain_current; - args[i].cont = &cont; + args[i].finished = &finished; + args[i].abort = &abort; pthread_create(&threads[i], NULL, compute_chunk, args+i); } + char prmpt[] = "To exit enter q, quit or exit.\nTo print current records to terminal enter r.\nTo print current records to a file enter p.\nTo do so with tex formatting enter t.\n"; + + pthread_t thread_handler; + Thread_handler_args thread_handler_args; + thread_handler_args.threads = threads; + thread_handler_args.message = prmpt; + pthread_create(&thread_handler, NULL, handle_threads, &thread_handler_args); + /*Capture user input.*/ size_t sz = 1024; - char prmpt[] = "To exit enter q, quit or exit.\nTo print current records to terminal enter r.\nTo print current records to a file enter p.\nTo do so with tex formatting enter t.\n"; char buff[sz]; + bool user_input_cont = true; do { getLine(prmpt, buff, sz); if( 0 == strcmp(buff,"q") || 0 == strcmp(buff,"quit") || 0 == strcmp(buff,"exit") ){ - print_records(exponents,records_ratio,records_index); - exit(EXIT_SUCCESS); + abort = true; + if(finished >= 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; } |
