Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6866863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:14:16+00:00 2026-05-27T03:14:16+00:00

I created a simple program that uses condition variables to create synchronization between two

  • 0

I created a simple program that uses condition variables to create synchronization between two threads. I’m getting a strange output that I cannot seem to find the solution to.

What the program does is, in the generator thread, produces 1000 random integers and checks to see if they are perfect squares. If the number is a perfect square then it signals the monitor thread which prints the square root of the number.

The problem I am having is most likely some sort of race condition, because the monitor simply isn’t printing out the square root when the generator signals.

The strange part is that when I debug in gdb b stepping through each time the variable is_square changes, the problem is nonexistent.

Any insight would appreciated. I feel it has to do with my mutex or placement of conditions.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <time.h>

int square_root;
int is_square = 0;
int done = 0;
int count = 0; //used to count how many perfect squares generator_func finds
int count1 = 0; //used to compare how many responses monitor makes to signal
pthread_mutex_t mutex;
pthread_cond_t mon;


void* generator_func(void* args){
  srand(time(NULL)); 
  int i, temp, sq;
  for(i = 0; i<1000; i++){
    temp = rand() % 10000;
    sq = sqrt((double)temp);
    if((pow(sq,2)) == temp){
      pthread_mutex_lock(&mutex);
      count++;
      square_root = sq;
      is_square = 1;
      fprintf(stderr, "Square root of %d is", temp);
      pthread_cond_signal(&mon);
      pthread_mutex_unlock(&mutex);
    } 
  }
  pthread_mutex_lock(&mutex);
  done = 1;
  is_square = -1;
  pthread_cond_signal(&mon);
  pthread_mutex_unlock(&mutex);
}


main(){
  pthread_t generator; //declare thread

  pthread_mutex_init(&mutex, NULL); //initialize mutex
  pthread_cond_init(&mon, NULL); //initialize condition variable


  pthread_create(&generator, NULL, generator_func, NULL); //create thread

  //monitor
  while(done != 1){
    pthread_mutex_lock(&mutex);
    while(is_square == 0){
      pthread_cond_wait(&mon, &mutex);
    }
    if(is_square == 1 && done != 1){
      count1++;
      fprintf(stderr, " %d\n", square_root);
      is_square = 0;
    }
    pthread_mutex_unlock(&mutex);
  }

  pthread_join(generator, NULL);

  printf("%d %d\n", count, count1); //shows inconsistency between generator and monitor
  pthread_mutex_destroy(&mutex);
  pthread_cond_destroy(&mon);
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:14:17+00:00Added an answer on May 27, 2026 at 3:14 am

    All,
    I have solved this problem very, very easily. I am embarrassed for not catching this sooner, but I took a break from this code for a while. The solution is so trivial. All that was needed was an extra condition variable that the generator waits on until the monitor is finished. Here is the code.

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <pthread.h>
    #include <time.h>
    
    int square_root;
    volatile int is_square = 0;
    int done = 0;
    int count = 0; //used to count how many perfect squares generator_func finds
    int count1 = 0; //used to compare how many responses monitor makes to signal
    pthread_mutex_t mutex;
    pthread_cond_t mon, gen;
    
    
    void* generator_func(void* args){
      srand(time(NULL)); 
      int i, temp, sq;
      for(i = 0; i<1000; i++){
        temp = rand() % 10000;
        sq = sqrt((double)temp);
        if((pow(sq,2)) == temp){
          pthread_mutex_lock(&mutex);
          count++;
          square_root = sq;
          is_square = 1;
          fprintf(stderr, "Square root of %d is", temp);
          pthread_cond_signal(&mon); //signal monitor
          pthread_cond_wait(&gen, &mutex); //wait for monitor to finish *Solution*
          pthread_mutex_unlock(&mutex);
        } 
      }
      pthread_mutex_lock(&mutex);
      done = 1;
      is_square = -1;
      pthread_cond_signal(&mon);
      pthread_mutex_unlock(&mutex);
    }
    
    
    main(){
      pthread_t generator; //declare thread
    
      pthread_mutex_init(&mutex, NULL); //initialize mutex
      pthread_cond_init(&mon, NULL); //initialize condition var for monitor
      pthread_cond_init(&gen, NULL); //initialize condition var for generator
    
      pthread_create(&generator, NULL, generator_func, NULL); //create thread
    
      //monitor
      pthread_mutex_lock(&mutex);
      while(done != 1 || is_square == 1){
        while(is_square == 0){
          pthread_cond_wait(&mon, &mutex);
        }
        if(is_square == 1 && done != 1){
          count1++;
          fprintf(stderr, " %d\n", square_root);
          is_square = 0;
        }
        pthread_cond_signal(&gen); //start generator back up *Solution*
      }
      pthread_mutex_unlock(&mutex);
    
    
      pthread_join(generator, NULL);
    
      printf("%d %d\n", count, count1); //shows inconsistency between generator and monitor
      pthread_mutex_destroy(&mutex);
      pthread_cond_destroy(&mon);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a simple visual basic 6 program/database that uses ms access
I have created a very simple program that uses recursion. I'm using the g++
So I created a simple hello world program that uses ksoap2 on Android 2.3
I created a simple program that demonstrates the runtime error I'm getting with my
I'm getting a consistent crash when I create a simple app that uses a
I want to create an extremely simple iPhone program that will open a telnet
I am trying to create a program that will do some simple calculations, but
I have been trying to create a simple program with Python which uses OpenCV
I have a simple program that uses FindWindowEx & strncmp() inside a callback passed
I'm writing a program that uses a bunch of two-way bindings and the amount

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.