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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:09:49+00:00 2026-05-28T14:09:49+00:00

I have a simple application with a manager thread that spawns ten simple worker

  • 0

I have a simple application with a “manager” thread that spawns ten simple “worker” threads. I want all of the “worker” threads to block on the same condition variable (ie: condvar), and I want to manually signal all ten threads to wake up at the same time with a pthread_cond_broadcast call.

In the case of my application, it is possible for threads to suffer an error condition and terminate early, so it is possible that not all ten threads make it to the synchronization point.

One simple mechanism would be to create a pthread_barrier_t and have all ten threads call pthread_barrier_wait, and when all ten threads complete this call, they are all free to continue execution. However, this would require the threads being able to modify the number of threads the barrier requires to unblock. I don’t know if this can be safely modified.

Additionally, I want to guarantee all the still-working threads not start automatically like they would with a barrier, I want to manually start them with a pthread_cond_broadcast call instead. How would I guarantee that all the threads that are still alive (ideally ten) have blocked on the condvar before I made the broadcast call?

Thanks!

  • 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-28T14:09:50+00:00Added an answer on May 28, 2026 at 2:09 pm

    The following shows one way to do it, using a condition variable and a few other variables; though there may be better ways. The comments should show how it works. You’d have to modify things to suit your actual situation of course; for instance there might be loops involved, etc.

    int activeThreads = 0;   /* number of threads currently going */
    int waitingThreads = 0;  /* number of threads waiting on the condition */
    int readyFlag = 0;       /* flag to tell the threads to proceed when signaled */
    pthread_cond_t  cond;    /* condition to wait on / signal */
    pthread_mutex_t mtx;     /* mutex for the above */
    
    pthread_cond_t  condWaiting; /* EDIT: additional condition variable to signal 
                                  * when each thread starts waiting */
    
    void *threadFunc(void *arg)
    {
      /* Edit: Rather than incrementing 'activeThreads' here, it should be done
       * in the main thread when each thread is created (to avoid a race) */
    
      /* ...do stuff... */
    
      /* When the threads should wait, do this (they wait for 'readyFlag' to be 
       * set, but also adjust the waiting thread count so the main thread can
       * determine whether to broadcast) */
      pthread_mutex_lock(&mtx);
        if (readyFlag == 0) {
          waitingThreads++;
          do {
            pthread_cond_signal(&condWaiting); /* EDIT: signal the main thread when
                                                * a thread begins waiting */
            pthread_cond_wait(&cond,&mtx);
          } while (readyFlag == 0);
          waitingThreads--;
        }
      pthread_mutex_unlock(&mtx);
    
      /* ...more stuff... */
    
      /* When threads terminate, they decrement the active thread count */
      pthread_mutex_lock(&mtx);
        activeThreads--;
        pthread_cond_signal(&condWaiting); /* EDIT: also signal the main thread
                                            * when a thread exits to make it 
                                            * recheck the waiting thread count if
                                            * waiting for all threads to wait */
      pthread_mutex_unlock(&mtx);
    
      return NULL;
    }
    
    int main(int argc, char *argv[])
    {
      /* Edit: Showing some code to initialize the mutex, condition variable(s),
       * etc., and create some threads -- modify as needed */
      pthread_mutex_init(&mtx,NULL);
      pthread_cond_init(&cond,NULL);
      pthread_cond_init(&condWaiting,NULL); /* EDIT: if main thread should block
                                             * until all threads are waiting */
      activeThreads = waitingThreads = readyFlag = 0;
    
      /* Edit: Increment 'activeThreads' here rather than in the thread function,
       * to avoid a race (if the main thread started waiting for the others
       * when not all had incremented the count yet, the main thread might end
       * up waiting for fewer threads to be ready -- though it's unlikely */
      #define NUM_THREADS 10
      pthread_t workers[NUM_THREADS];
      for (int i = 0; i < NUM_THREADS; i++) {
        /* should use appropriate thread attributes */
        if (pthread_create(&workers[i],NULL,threadFunc,NULL) == 0)
          activeThreads++;
      }
    
      /* ...do stuff... */
    
      /* Set 'readyFlag' and do condition broadcast IF all threads are waiting,
       * or just carry on if they aren't */
      pthread_mutex_lock(&mtx);
        if ((activeThreads != 0) && (activeThreads == waitingThreads)) {
          readyFlag = 1;
          pthread_cond_broadcast(&cond);
        }
      pthread_mutex_unlock(&mtx);
    
      /* EDIT: OR.. to wait until all threads are waiting and then broadcast, do 
       * this instead: */
      pthread_mutex_lock(&mtx);
        while (waitingThreads < activeThreads) { /* wait on 'condWaiting' until all
                                                  * active threads are waiting */
          pthread_cond_wait(&condWaiting,&mtx);
        }
        if (waitingThreads != 0) {
          readyFlag = 1;
          pthread_cond_broadcast(&cond);
        }
      pthread_mutex_unlock(&mtx);
    
      /* ...more stuff... */
    
      /* If needed, you can clear the flag when NO threads are waiting.. */
      pthread_mutex_lock(&mtx);
        if (waitingThreads == 0)
          readyFlag = 0;
      pthread_mutex_unlock(&mtx);
    
      /* ...even more stuff... */
    
      return 0;
    }
    

    I’d add, though, that I don’t see when there’d be a good reason to do this rather than just protecting resources in a more straightforward way.

    EDIT: Added some things to the code, showing a second condition variable used to let the main thread wait for all the workers to be ready. The changed parts are marked with “EDIT:” in comments, and can be left out if not needed. I’ve also corrected a race condition by moving the increment of activeThreads out of the thread function, and shown the initialization for the mutex, etc. (without error handling).

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

Sidebar

Related Questions

I have a relatively simple program that currently uses 1 thread (main). The application
I have written a simple contact manager application that uses a UITableView to display
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have a simple application that loads an unmanaged dll and passes a few
I have a simple application written in C# and .Net 2.0 that displays several
I have a security manager in my application that works for both windows and
I have an application which spawns a new thread when a user asks for
If I have a data structure in a multithreaded application that I want to
I have developed an simple android application that says hello. Now i have transferred
I have a simple application that fetches some data from ONE table on db

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.