views:

32

answers:

1

Hi everyone,

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 :)

+3  A: 

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.

Hans Passant
Thanks, that did the trick, which is obvious that it should have..
redeye
I initially created the threads with the CREATE_SUSPENDED flag, then call ResumeThread _after_ the initialization code. When i changed the CreateThread call to create them so they start immediately, i forgot to move to also move the condition init code up. My bad :)
redeye