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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:28:23+00:00 2026-05-20T13:28:23+00:00

We can use either the new condition variable primitive or windows event in order

  • 0

We can use either the new condition variable primitive or windows event in order to synchronize threads in WinNT v6.x or later. Consider the following two approaches, we want workers to run at the same time when “go” is set in main, otherwise they should all block.

/*language C code*/
/*Windows Condition Variable*/
int go=0;
CONDITION_VARIABLE cv;
SRWLOCK lock;
void workers()
{
    AcquireSRWLockShared(&lock);
    if(go==0)
    {
        SleepConditionVariableSRW(&cv, &lock, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED);
    }
    ReleaseSRWLockShared(&lock);
    /*
    Workers continue...
    */
}
void main()
{
    int i;
    InitializeConditionVariable(&cv);
    InitializeSRWLock(&lock);
    for(i=0;i<10;i++)
    {
        CreateThread(0, 0, workers, 0, 0, 0);
    }
    AcquireSRWLockExclusive(&lock);
    go=1;
    ReleaseSRWLockExclusive(&lock);
    WakeAllConditionVariable(&cv);
}

or

/*language C code*/
/*Windows Event*/
HANDLE go;
void workers()
{
    WaitForSingleObject(go, INFINITE);
    /*
    Workers continue...
    */
}
void main()
{
    int i;
    go=CreateEvent(0,1,0,0); /*No security descriptor, Manual Reset, initially 0, no name*/
    for(i=0;i<10;i++)
    {
        CreateThread(0, 0, workers, 0, 0, 0);
    }
    SetEvent(go);
}

In the first approach, workers are blocked on SleepConditionVariableSRW and woke up by WakeAllConditionVariable. In the second, they are blocked on WaitForSingleObject and woke up by SetEvent.

Which one is better in practice, only regarding overhead? (hint: context switch, lock contention, overhead of blocking threads)

I would choose the first but feel lack of justification.

  • 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-20T13:28:24+00:00Added an answer on May 20, 2026 at 1:28 pm

    This particular use case is ideal for an event: it’s a one-shot process, where all waiting threads must be woken.

    Condition variables are better suited for things like queues, where there is an associated predicate that may or not be true when the waiting thread wakes up (such as items on the queue — they may have been consumed by another thread), or where it matters that the thread woken is one of those already waiting when the condition variable is notified, rather than one that comes along afterwards.

    Plus, as Hans pointed out, Windows native condition variables only work on Vista or later, so you can’t use them if compatibility with Windows XP is a concern.

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

Sidebar

Related Questions

We have got a setup where users can either use a web-based form or
On Unix, I can either use \r (carriage return) or \b (backspace) to overwrite
How can I configure an application, or even an entire machine, to use either
in the tutorial i can use either: public function configure() { $this->setWidgets(array( 'type' =>
You can use more than one css class in an HTML tag in current
You can use a standard dot notation or a method call in Objective-C to
I can use properties of an Excel Worksheet to tell if the worksheet is
You can use ftplib for full FTP support in Python. However the preferred way
You can use SelectFolder() to get a folder or GetOpenFolderitem(filter as string) to get
You can use App.config; but it only supports key/value pairs. You can use .Net

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.