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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:00:52+00:00 2026-06-13T14:00:52+00:00

So basically the situation I am in is I have a bunch of threads

  • 0

So basically the situation I am in is I have a bunch of threads each doing different calculations throughout the week. At the end of the week, every thread calls function X() and then starts calculating for the next week and repeats this cycle.

However, only one thread is allowed to actually do the operations in method X() and only when all threads have reached method X(). Furthermore, none of the threads can continue on their way until the one thread that got to use method X() is finished.

So I’m having difficulty implementing this. I feel like I need to use a condition variable but I’m still shaky with threads and whatnot.

  • 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-13T14:00:53+00:00Added an answer on June 13, 2026 at 2:00 pm

    Barriers are a useful synchronization method here.

    In pthreads, you can use two barriers, each initialized to a require however many threads are running. The first synchronizes threads after they’ve finished calculating, and the second after one of them has called X(). Conveniently, the pthread_barrier_wait will elect one and only one of your N waiting threads to actually call X():

    void *my_thread(void *whatever) { // XXX error checking omitted
      while (1) {
        int rc;
    
        do_intense_calculations();
    
        // Wait for all calculations to finish
        rc = pthread_barrier_wait(&calc_barrier);
    
        // Am I nominated to run X() ?
        if (rc == PTHREAD_BARRIER_SERIAL_THREAD) X();
    
        // Wait for everyone, including whoever is doing X()
        rc = pthread_barrier_wait(&x_barrier);
    }
    

    Java’s CyclicBarrier with a Runnable argument would let you do the same thing with but one barrier. (The Runnable is run after all parties arrive but before any are released.)

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

Sidebar

Related Questions

I'm basically stuck in a situation where I need a bunch of div's with
Basically the situation is like this (simplified, pseudo code): I have 2 TABLES person
I have a situation that users access remote MySQL server in C# application. Basically,
I have a situation where a vendor-supplied assembly reference is messed up. Basically, it
Here is the situation : I have a vertical menu with div. Each div
Basically I have a situation like this: $search = array( '?', '?' ); $replace
The Situation I have a table in a DB that contains job types, basically
The Situation I have a very compressed time schedule to write a simple (basically
I have this situation (two classes with two different header files): b.h #include a.h
I have a situation that relies on knowing when the job completes. Basically a

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.