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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:34:29+00:00 2026-05-17T23:34:29+00:00

I have a problem in understanding how the winapi condition variables work. On the

  • 0

I have a problem in understanding how the winapi condition variables work.

On the more specific side, what I want is a couple of threads waiting on some condition. Then I want to use the WakeAllConditionVariable() call to wake up all the threads so that they can do work. Besides the fact that i just want the threads started, there isn’t any other prerequisite for them to start working ( like you would have in an n producer / n consumer scenario ).

Here’s the code so far:

#define MAX_THREADS 4

CONDITION_VARIABLE  start_condition;
SRWLOCK            cond_rwlock;
bool                  wake_all;

__int64 start_times[MAX_THREADS];

Main thread:

int main() 
{
    HANDLE h_threads[ MAX_THREADS ];

    int tc;
    for (tc = 0; tc < MAX_THREADS; tc++)
    {
        DWORD tid;
        h_threads[tc] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)thread_routine,(void*)tc,0,&tid);
        if( h_threads[tc] == NULL )
        {
            cout << "Error while creating thread with index " << tc << endl;
            continue;
        }
    }

    InitializeSRWLock( &cond_rwlock );
    InitializeConditionVariable( &start_condition );

    AcquireSRWLockExclusive( &cond_rwlock );
        // set the flag to true, then wake all threads
    wake_all = true;
    WakeAllConditionVariable( &start_condition );

    ReleaseSRWLockExclusive( &cond_rwlock );

    WaitForMultipleObjects( tc, h_threads, TRUE, INFINITE );

    return 0;
}

And here is the code for the thread routine:

DWORD thread_routine( PVOID p_param )
{
    int t_index = (int)(p_param);

    AcquireSRWLockShared( &cond_rwlock );

        // main thread sets wake_all to true and calls WakeAllConditionVariable()
        // so this thread should start doing the work (?)
    while ( !wake_all )
        SleepConditionVariableSRW( &start_condition,&cond_rwlock, INFINITE,CONDITION_VARIABLE_LOCKMODE_SHARED );

    QueryPerformanceCounter((LARGE_INTEGER*)&start_times[t_index]);
        // do the actual thread related work here

    return 0;
}

This code does not do what i would expect it to do. Sometimes just one thread finishes the job, sometimes two or three, but never all of them. The main function never gets past the WaitForMultipleObjects() call.

I’m not exactly sure what I’ve done wrong, but I would assume some synchronization issue somewhere ?

Any help would be appreciated. (sorry if I re-posted older topic with different dressing 🙂

  • 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-17T23:34:30+00:00Added an answer on May 17, 2026 at 11:34 pm

    You initialize the cond_rwlock and start_condition variables too late. Move the code up, before you start the threads. A thread is likely to start running right away, especially on a multi-core machine.

    And test the return values of api functions. You don’t know why it doesn’t work because you never check for failure.

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

Sidebar

Related Questions

i'm starting to work with php basics and i have some problem understanding how
I am facing a situation that I have problem understanding... I am writing a
i have small problem understanding why my smart pointer class is leaking on self
I have a problem understanding what's causes the compilation error in the code below:
I seem to have a problem understanding how to conditionally test a boolean value
I have a couple search forms, 1 with ~50 fields and the other with
For some time I’ve seen employers demanding Sharepoint knowledge from programmers, but I have
I have been doing a lot of searching and still can't seem to figure
I am working on an Android service. I need to call RecognizerIntent from a
According to the XML spec, this is the definition of an empty element: An

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.