Hello everyone.I have a problem.
(In main): I create 5 threads that put 1 item (at a time) in a rack and 5 threads that get 3 items (at a time) from the rack.
So,the threads that get items ,have to wait the rack to fill and when the rack has >= 3 items send a signal to the other threads in order to wake up and continue the job.This happens 10 times (for loop inside put_on_the_rack() ).
So,when I run the code,this happens:
nikos@nick-ubuntu:~/Desktop$ gcc cond_var__.c -o cond_var__ -lpthread
nikos@nick-ubuntu:~/Desktop$ ./cond_var__
Putting items in the rack: 1
Putting items in the rack: 2
Putting items in the rack: 3
Putting items in the rack: 4
Putting items in the rack: 5
Getting items from the rack. Items left: 2
Waiting the rack to full...
Waiting the rack to full...
Waiting the rack to full...
Waiting the rack to full...
Putting items in the rack: 3
Putting items in the rack: 4
Getting items from the rack. Items left: 1
Putting items in the rack: 2
Waiting the rack to full...
Putting items in the rack: 3
Getting items from the rack. Items left: 0
Putting items in the rack: 1
Waiting the rack to full...
Waiting the rack to full...
Putting items in the rack: 2
Putting items in the rack: 3
Getting items from the rack. Items left: 0
Putting items in the rack: 1
Putting items in the rack: 2
Waiting the rack to full...
Putting items in the rack: 3
Getting items from the rack. Items left: 0
Putting items in the rack: 1
Putting items in the rack: 2
Putting items in the rack: 3
Putting items in the rack: 4
Putting items in the rack: 5
Putting items in the rack: 6
Putting items in the rack: 7
Putting items in the rack: 8
Putting items in the rack: 9
Putting items in the rack: 10
Putting items in the rack: 11
Putting items in the rack: 12
Putting items in the rack: 13
Putting items in the rack: 14
Putting items in the rack: 15
Putting items in the rack: 16
Putting items in the rack: 17
Putting items in the rack: 18
Putting items in the rack: 19
Putting items in the rack: 20
Putting items in the rack: 21
Putting items in the rack: 22
Putting items in the rack: 23
Putting items in the rack: 24
Putting items in the rack: 25
Putting items in the rack: 26
Putting items in the rack: 27
Putting items in the rack: 28
Putting items in the rack: 29
Putting items in the rack: 30
Putting items in the rack: 31
Putting items in the rack: 32
Putting items in the rack: 33
Putting items in the rack: 34
Putting items in the rack: 35
nikos@nick-ubuntu:~/Desktop$
So, my question is ,why threads that get the items don't wake up in order to do the job?
I don't understand what is happening at the end.Provided that I have 35 items ,why threads don't do their job (get the items from the rack).
I miss something here and I want your help to understand and fix it.
Thanks a lot.
Code:
What I have tried:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
int num_of_items = 0;
pthread_mutex_t mutex_item__;
pthread_cond_t cond_item;
void *put_on_the_rack(void *arg)
{
for(int i = 0; i < 10; i++) {
pthread_mutex_lock(&mutex_item__);
num_of_items++;
printf("Putting items in the rack: %d\n" , num_of_items);
pthread_mutex_unlock(&mutex_item__);
pthread_cond_broadcast(&cond_item);
sleep(1);
}
}
void *get_from_the_rack(void *arg)
{
pthread_mutex_lock(&mutex_item__);
while(num_of_items < 3)
{
printf("Waiting the rack to full...\n");
pthread_cond_wait(&cond_item , &mutex_item__);
}
num_of_items -= 3;
printf("Getting items from the rack. Items left: %d\n" , num_of_items);
pthread_mutex_unlock(&mutex_item__);
}
int main()
{
pthread_t thread_put[5] , thread_get[5];
pthread_cond_init(&cond_item , NULL);
pthread_mutex_init(&mutex_item__ , NULL);
for(int i = 0; i < 5; i++)
{
pthread_create(&thread_put[i] , NULL , &put_on_the_rack , NULL);
}
for(int i = 0; i < 5; i++)
{
pthread_create(&thread_get[i] , NULL , &get_from_the_rack , NULL);
}
for(int i = 0; i < 5; i++)
{
pthread_join(thread_put[i] , NULL);
}
for(int i = 0; i < 5; i++)
{
pthread_join(thread_get[i] , NULL);
}
return 0;
}