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

  • Home
  • SEARCH
  • 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 6745583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:13:21+00:00 2026-05-26T12:13:21+00:00

My intention is to let send() omit signal when the global counter is odd,

  • 0

My intention is to let send() omit signal when the global counter is odd, and broadcast when even. Then there are 2 recv() thread, waiting for signal. In case of signal, only one of the recv() thread can receive it; in case of broadcast, both may receive it.

But my result shows me that both recv() received the same odd number, which should not happen.

Is the global counter and the signal tightly coupled in my code?

edit:

I think I get answer to this question. After a recv() thread receives signal, it will try to lock the mutex associated with the condition variable again, but it may need to wait because the mutex may have already been locked by other thread. During the waiting period, the global count may be changed, so the global count it finally prints is different from the one when it received signal.

Also, in my code, even send() send broadcast, multiples recv() threads actually can not handle the signal concurrently, because they are still exclusive to each other because they share the same mutex

end of edit

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int n = 0;
int count = 0;

void* recv(void *name)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);

        // wait for signal
        pthread_cond_wait(&cond, &mutex);
        count++;

        if(count > 10)
        {
            printf("number %d terminate %s\n", n, (char*)name);
        }
        else
        {
            printf("%s gets number:%d\n", (char*)name, n);
        }
        pthread_mutex_unlock(&mutex);

        if(count > 10)
        {
            return (NULL);
        }
    }
}

void* send(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        n++;
        //  odd: signal
        // even: broadcast
        if(n%2)
            pthread_cond_signal(&cond);
        else
            pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&mutex);
    }
}


int main(int argc, char *argv[])
{    
    pthread_t t1, t2, t3;

    char *name1 = "t1";
    char *name2 = "t2";

    pthread_create(&t1, NULL, recv, (void *) name1);
    pthread_create(&t2, NULL, recv, (void *) name2);
    pthread_create(&t3, NULL, send, NULL);

    pthread_join(t1, NULL);
        pthread_join(t2, NULL);

    pthread_cancel(t3);

    return 0;
}

Output, note the first 2 lines:

t2 gets number:12493
t1 gets number:12493
t1 gets number:12534
t2 gets number:12534
t1 gets number:12623
t1 gets number:12624
t2 gets number:12666
t1 gets number:12666
t2 gets number:12713
t1 gets number:12713
number 12770 terminate t2
number 12998 terminate t1
  • 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-26T12:13:21+00:00Added an answer on May 26, 2026 at 12:13 pm

    After a broadcast wakeup, there is no guarantee that the threads will be scheduled as:

    t1
    t2
    t3
    

    or

    t2
    t1
    t3
    

    It is entirely possible that you get:

    t1
    t3
    t2
    

    …in which t3 increments n to an odd value and wakes up t1, and t2 and t1 then both print that odd number.

    If you want to closely interlock execution like this you will need to introduce more synchronisation.

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

Sidebar

Related Questions

My intention is for a new <input> fields to be created every time there
I Produced a page which I have no intention to let Search Engines find
Let me first describe the picture below: There are two printed papers. The only
Let's say I'm writing a global logData method that wants to write to a
Let's say I have a class that requires configuration, dependency injection etc. public class
Unlike HashMap, order matters in LinkedHashMap. And the order here is insertion order. Let
My intention is to have 4 main Nav-bars at a site. If the user
The intention is to build a wrapper to provide a consistent method of calling
My intention is to read every elements in the 2nd column into a buffer[]
My intention is to embed the JFileChooser in an other component, for instance, one

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.