diff options
| author | Julian Weigt <juw@posteo.de> | 2026-02-10 08:46:07 +0100 |
|---|---|---|
| committer | Julian Weigt <juw@posteo.de> | 2026-02-10 08:46:07 +0100 |
| commit | 3f84820a2c0be6333c14740052e243efd06b6a9e (patch) | |
| tree | da0ffb3f7686fe30d2c6c30ccb97fff12e474f21 | |
| parent | 1f441038b320cd30d81437ce54c65ee9f1158142 (diff) | |
Make thread handler more involved in aborting.
| -rw-r--r-- | charf.c | 22 |
1 files changed, 12 insertions, 10 deletions
@@ -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<P; p++) records_ratio[k][p] = int_to_valuetype(0); int domain_current = 0; - int finished = 0; - bool abort = false; + bool cont = true; + bool is_computing = true; /*Start threads that do the computation.*/ for (int i = 0; i < NUM_THREADS; i++) { @@ -575,8 +577,7 @@ int main() { args[i].records_index = &records_index; args[i].num_thread = i; args[i].domain_current = &domain_current; - args[i].finished = &finished; - args[i].abort = &abort; + args[i].cont = &cont; pthread_create(&threads[i], NULL, compute_chunk, args+i); } @@ -586,6 +587,7 @@ int main() { Thread_handler_args thread_handler_args; thread_handler_args.threads = threads; thread_handler_args.message = prmpt; + thread_handler_args.is_computing = &is_computing; pthread_create(&thread_handler, NULL, handle_threads, &thread_handler_args); /*Capture user input.*/ @@ -595,9 +597,9 @@ int main() { do { getLine(prmpt, buff, sz); if( 0 == strcmp(buff,"q") || 0 == strcmp(buff,"quit") || 0 == strcmp(buff,"exit") ){ - if(finished < NUM_THREADS){ + if(is_computing){ printf("Aborting calculations.\n"); - abort = true; + cont = false; } else{ printf("Stopping program.\n"); |
