Click here to Skip to main content
15,559,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
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:

C++
#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++) // put 10 times
    {
        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;
}
Posted
Updated 10-Sep-21 8:46am

The function get_from_the_rack() returns after extracting 3 items, so at most you will remove 15 items from the stack. You need to either wrap the body of get_from_rack() in a loop that tests for some sort of end-of-processing condtions (e.g. perhaps no more items on the rack, and all put threads have ended), or restart the get threads from main, when the exit - maybe using something like pthread_tryjoin_np()
 
Share this answer
 
Comments
Nick_is_asking 10-Sep-21 10:22am    
I don't know how to do that.if anyone can help me ,I would appreciate it.
When you get from the reck it gets empty and your get function is exiting. Than you only fill the reck.

C++
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__);
    // here is the last command of your thread
    printf("Now existing the thread. BYE BYE");
}
tip: write the whole get code into a while loop with some exit condition like a global mutex
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900