Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.I have a small program.I have to create 2 threads in order to increase a global variable until 20 (for example).At the same time I want my 3rd thread
(3 threads total) to wait until global variable reach the number 20.if number is 20,then I want my 3rd thread to print a message ("I'm awake").
I did some things ,but I'm stuck where the phtread_cond_wait must be(in code)...

What I have tried:

C++
#include <pthread.h>
#include <stdio.h>

pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *func(void *arg);
int g = 1;

int main()
{
    pthread_t thr[2];
    pthread_t thr_w;
    
    
    for(int i = 0; i < 2; i++)
    {
        pthread_create(&thr[i] , NULL , func , &g);
    }

    for(int i = 0; i < 2; i++)
    {
        pthread_join(thr[i] , NULL);
        pthread_cond_signal(&cond);
    }
    
    printf("\nCount finished...\n");
    return 0;
}



void *func(void *arg)
{
    int local = *(int *)arg;

    for(int i = local; i <= 10; i++)
    {
        pthread_mutex_lock(&my_mutex);
        printf("g = %d\n" , g);
        g++;
        pthread_mutex_unlock(&my_mutex);
    }
}


This code just increase the global variable until 20,but I'm stuck on how to continue.I need help.

Thanks in advance.
Posted
Updated 10-Mar-21 20:34pm
Comments
KarstenK 11-Mar-21 1:24am    
What is your plan?

1 solution

Try, for instance (see Using Condition Variables (Multithreaded Programming Guide)[^])
C
#include <pthread.h>
#include <stdio.h>

pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void * inc_func(void * arg);
void * wait_for_twenty_func(void * arg);
int g = 1;

int main()
{
    pthread_t thr[2];
    pthread_t thr_w;


    for(int i = 0; i < 2; i++)
    {
        pthread_create(&thr[i] , NULL , inc_func , &g);
    }

     pthread_create(&thr_w , NULL , wait_for_twenty_func , NULL);


    for(int i = 0; i < 2; i++)
    {
        pthread_join(thr[i] , NULL);
        //pthread_cond_signal(&cond);
    }
    pthread_join(thr_w , NULL);

    printf("\nCount finished...\n");
    return 0;
}

void * inc_func(void * arg)
{
    for(int i = 0; i < 10; i++)
    {
        pthread_mutex_lock(&my_mutex);
        printf("g = %d\n" , g);
        if ( g < 20)
        {
          g++;
          pthread_cond_signal(&cond); // signal the condition
        }
        pthread_mutex_unlock(&my_mutex);
    }
  return NULL;
}

void * wait_for_twenty_func(void * arg)
{
  int is_twenty = 0;
  while (! is_twenty )
  {
    pthread_mutex_lock(&my_mutex);
    pthread_cond_wait(&cond, &my_mutex); // wait for the condition
    printf("wait expired\n");
    is_twenty = (g==20);

    pthread_mutex_unlock(&my_mutex);
  }
  printf("twenty reached\n");
  return NULL;
}
 
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