diff --git a/Code_With_Threads/main.c b/Code_With_Threads/main.c index 44b31300de7ff0a55669ff1bc0ae4ee0c6b05805..8dabc0ca73088a74a672a5bbeffffe36ea3f9100 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");