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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:49:25+00:00 2026-05-13T05:49:25+00:00

I have a main program which creates a collection of N child threads to

  • 0

I have a main program which creates a collection of N child threads to perform some calculations. Each child is going to be fully occupied on their tasks from the moment their threads are created till the moment they have finished. The main program will also create a special (N+1)th thread which has some intermittent tasks to perform. When certain conditions are met (like a global variable takes on a certain value) the special thread will perform a calculation and then go back to waiting for those conditions to be met again. It is vital that when the N+1th thread has nothing to do, it should not slow down the other processors.
Can someone suggest how to achieve this.

EDIT:
The obvious but clumsy way would be like this:

// inside one of the standard worker child threads...
if (time_for_one_of_those_intermittent_calculations_to_be_done())
{
    global_flag_set = TRUE;
}

and

// inside the special (N+1)th thread
for(;;)
{
    if (global_flag_set == TRUE)
    {
        perform_big_calculation();
        global_flag_set = FALSE;
    }
    // sleep for a while?
}
  • 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-13T05:49:26+00:00Added an answer on May 13, 2026 at 5:49 am

    A ready-to-use condition class for WIN32 😉

    class Condition {
    private:
        HANDLE m_condition;
        Condition( const Condition& ) {} // non-copyable
    public:
        Condition() {
            m_condition = CreateEvent( NULL, TRUE, FALSE, NULL );
        }
        ~Condition() {
            CloseHandle( m_condition );
        }
        void Wait() {
            WaitForSingleObject( m_condition, INFINITE );
            ResetEvent( m_condition );
        }
        bool Wait( uint32 ms ) {
            DWORD result = WaitForSingleObject( m_condition, (DWORD)ms );
            ResetEvent( m_condition );
            return result == WAIT_OBJECT_0;
        }
        void Signal() {
            SetEvent( m_condition );
        }
    };
    

    Usage:

    // inside one of the standard worker child threads...
    if( time_for_one_of_those_intermittent_calculations_to_be_done() ) {
        global_flag_set = TRUE;
        condition.Signal();
    }
    
    
    // inside the special (N+1)th thread
    for(;;) {
        if( global_flag_set==FALSE ) {
            condition.Wait(); // sends thread to sleep, until signalled
        }
        if (global_flag_set == TRUE) {
            perform_big_calculation();
            global_flag_set = FALSE;
        }
    }
    

    NOTE: you have to add a lock (e.g. a critical section) around global_flag_set. And also in most cases the flag should be replaced with a queue or at least a counter (a thread could signal multiple times while ‘special’ thread is performing its calculations).

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

Sidebar

Related Questions

I have a main program written in C# which creates and uses objects written
I currently have two threads running in my program: Main thread - Grabs image
I have a C# console program which main functions should let a user grep
I have called a native program which creates another thread, which attaches itself to
I have a main program, in which GUI is based on swing and depending
I have a C++ program in which I have multiple threads writing into my
I have a Python program that consists of several modules. The main module creates
I have an existing C++ win32 console app. This application contains a main program
I have a main class in a program that launches another class that handles
We are installing out program via inno setup. We have one main exe file,

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.