From e2d3970b9405d7b996863fb0c3b3c10d260403cd Mon Sep 17 00:00:00 2001 From: Arnaud Lefebvre <arnaud.lefebvre@student.uclouvain.be> Date: Thu, 14 May 2020 12:13:54 +0200 Subject: [PATCH] Replace main.c --- Code_With_Threads/main.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/Code_With_Threads/main.c b/Code_With_Threads/main.c index 44b3130..8dabc0c 100644 --- a/Code_With_Threads/main.c +++ b/Code_With_Threads/main.c @@ -17,8 +17,7 @@ char *f_out; int nthreads = 1; buffer1 *buffer_1; buffer2 *buffer_2; -int nlines1; -int nlines2; +int nlines; void *reading() { @@ -56,9 +55,7 @@ void *reading() { void *calculating() { - int nlines1 = count_lines(f_in); - while ((buffer_1->can_stop == false || buffer_1->numbers->length != 0) && nlines1 != 0) { - nlines1 --; + while (buffer_1->can_stop == false || buffer_1->numbers->length != 0) { uint64_t number; sem_wait(&(buffer_1->full)); @@ -76,17 +73,13 @@ void *calculating() { sem_post(&(buffer_2->full)); } - pthread_mutex_unlock(&(buffer_2->mutex)); - buffer_2->can_stop = true; - pthread_mutex_unlock(&(buffer_2->mutex)); - return NULL; } void *writing() { - int nlines2 = count_lines(f_in); - while((buffer_2->can_stop == false || buffer_2->prime_numbers->length > 0) && nlines2 != 0) { - nlines2 --; + int nlines = count_lines(f_in); + while (nlines > 0) { + nlines --; sem_wait(&(buffer_2->full)); pthread_mutex_lock(&(buffer_2->mutex)); @@ -165,7 +158,6 @@ int main(int argc, char *argv[]) { printf("Read_thread_join OK.\n"); } - for (int i = 0; i < nthreads; i++) { if (pthread_cancel(calc[i]) != 0) { printf("Error with cancel calculating thread[%d].\n", i); @@ -173,7 +165,7 @@ int main(int argc, char *argv[]) { printf("Calc_thread_cancel[%d].\n", i); } } - + for (int i = 0; i < nthreads; i++) { if (pthread_join(calc[i], NULL) != 0) { printf("Error with join calculating thread.\n"); -- GitLab