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 8940145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:58:19+00:00 2026-06-15T10:58:19+00:00

I’m trying to implement pthread_cond_wait for 2 threads. My test code is trying to

  • 0

I’m trying to implement pthread_cond_wait for 2 threads. My test code is trying to use two threads to preform the following scenario:

  • Thread B waits for condition
  • Thread A prints “Hello” five times
  • Thread A signals thread B
  • Thread A waits
  • Thread B prints “Goodbye”
  • Thread B signals thread A
  • Loop to start (x5)

So far the code prints “Hello” five times and then gets stuck. From examples I’ve looked at it seems I’m on the right track, “Lock mutex, wait, get signaled by other thread, unlock mutex, do stuff, loop”

Test Code:

//Import 
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

//global variables
pthread_cond_t      condA  = PTHREAD_COND_INITIALIZER;
pthread_cond_t      condB  = PTHREAD_COND_INITIALIZER;
pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;




void *threadA()
{
    int i = 0, rValue, loopNum;

    while(i<5)
    {
        //unlock mutex
        rValue = pthread_mutex_unlock(&mutex);

        //do stuff
        for(loopNum = 1; loopNum <= 5; loopNum++)
            printf("Hello %d\n", loopNum);

        //signal condition of thread b
        rValue = pthread_cond_signal(&condB);

        //lock mutex
        rValue = pthread_mutex_lock(&mutex);

        //wait for turn
        while( pthread_cond_wait(&condA, &mutex) != 0 )

        i++;
    }

}



void *threadB()
{
    int n = 0, rValue;

    while(n<5)
    {
        //lock mutex
        rValue = pthread_mutex_lock(&mutex);

        //wait for turn
        while( pthread_cond_wait(&condB, &mutex) != 0 )

        //unlock mutex
        rValue = pthread_mutex_unlock(&mutex);

        //do stuff
        printf("Goodbye");

        //signal condition a
        rValue = pthread_cond_signal(&condA);

        n++;        
    }
}




int main(int argc, char *argv[])
{
    //create our threads
    pthread_t a, b;

    pthread_create(&a, NULL, threadA, NULL);
    pthread_create(&b, NULL, threadB, NULL);

    pthread_join(a, NULL);
    pthread_join(b,NULL);
}

A pointer in the right direction would be greatly appreciated, thanks!
(Code compiled on Linux using “gcc timeTest.c -o timeTest -lpthread”)

  • 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-06-15T10:58:20+00:00Added an answer on June 15, 2026 at 10:58 am

    You have two problems. The first is that you aren’t using while() loops correctly – for example, here:

    //wait for turn
    while( pthread_cond_wait(&condA, &mutex) != 0 )
    
    i++;
    

    The body of the while loop is the statement i++ – this will execute pthread_cond_wait() and i++ until the pthread_cond_wait() returns an error, so this is essentially an endless loop.

    The second is that you can’t use a pthreads condition variable on its own – it needs to be paired with some actual shared state (at its simplest, this shared state might just be a flag variable protected by a mutex). The pthread_cond_wait() function is used to wait for the shared state to reach a certain value, and the pthread_cond_signal() function is used when a thread has altered the shared state. Reworking your example to use such a variable:

    //global variables
    /* STATE_A = THREAD A runs next, STATE_B = THREAD B runs next */
    enum { STATE_A, STATE_B } state = STATE_A;
    pthread_cond_t      condA  = PTHREAD_COND_INITIALIZER;
    pthread_cond_t      condB  = PTHREAD_COND_INITIALIZER;
    pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;
    
    void *threadA()
    {
        int i = 0, rValue, loopNum;
    
        while(i<5)
        {
            /* Wait for state A */
            pthread_mutex_lock(&mutex);
            while (state != STATE_A)
                pthread_cond_wait(&condA, &mutex);
            pthread_mutex_unlock(&mutex);
    
            //do stuff
            for(loopNum = 1; loopNum <= 5; loopNum++)
                printf("Hello %d\n", loopNum);
    
            /* Set state to B and wake up thread B */
            pthread_mutex_lock(&mutex);
            state = STATE_B;
            pthread_cond_signal(&condB);
            pthread_mutex_unlock(&mutex);
    
            i++;
        }
    
        return 0;
    }
    
    void *threadB()
    {
        int n = 0, rValue;
    
        while(n<5)
        {
            /* Wait for state B */
            pthread_mutex_lock(&mutex);
            while (state != STATE_B)
                pthread_cond_wait(&condB, &mutex);
            pthread_mutex_unlock(&mutex);
    
            //do stuff
            printf("Goodbye\n");
    
            /* Set state to A and wake up thread A */
            pthread_mutex_lock(&mutex);
            state = STATE_A;
            pthread_cond_signal(&condA);
            pthread_mutex_unlock(&mutex);
    
            n++;
        }
    
        return 0;
    }
    

    Note that the use of two condition variables condA and condB is unnecessary here – the code would be just as correct if only one condition variable was used instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

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.