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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:46:43+00:00 2026-05-30T20:46:43+00:00

It is first time when I’m synchronizing threads using events. My code works fine.

  • 0

It is first time when I’m synchronizing threads using events. My code works fine. But as I experienced it can look fine but have obvious bug which will be really hard to find when I use this concept in bigger project.

So I just want to ask if this way of using events to synchronize threads looks fine for you?

The idea is that we have GetSymbol function which can be called only from main thread. Server thread need to ask main thread for results from this function.

#include <windows.h>
#include <process.h>
#include <stdio.h>

HANDLE symbol_need, symbol_ready, end;

int symbol_container;

int GetSymbol()
{
    // Only main thread can use this function.
    static int i = 0;
    return ++i;
}

void Server(void* p)
{
    printf("Ask for first symbol.\n");
    SetEvent(symbol_need);

    DWORD wait_result;

    wait_result = WaitForSingleObject(symbol_ready, INFINITE);
    if(WAIT_OBJECT_0 == wait_result)
    {
        ResetEvent(symbol_ready);
        printf("First symbol: %i\n", symbol_container);
    } else {
        printf("Something went wrong.\n");
    }

    printf("Ask for second symbol.\n");
    SetEvent(symbol_need);

    wait_result = WaitForSingleObject(symbol_ready, INFINITE);
    if(WAIT_OBJECT_0 == wait_result)
    {
        ResetEvent(symbol_ready);
        printf("Second symbol: %i\n", symbol_container);
    } else {
        printf("Something went wrong.\n");
    }

    printf("OK, finish it.");
    SetEvent(end);
}


int main(int argc, char* argv[])
{
    symbol_need = CreateEvent( NULL, FALSE, FALSE, NULL );
    symbol_ready = CreateEvent( NULL, FALSE, FALSE, NULL );
    end = CreateEvent( NULL, FALSE, FALSE, NULL );

    _beginthread(Server, 0, NULL);

    DWORD wait_result;

    while(1)
    {
        wait_result = WaitForSingleObject(symbol_need, 100);
        if(WAIT_OBJECT_0 == wait_result)
        {
            ResetEvent(symbol_need);
            symbol_container = GetSymbol();
            SetEvent(symbol_ready);
        }
        wait_result = WaitForSingleObject(end, 100);
        if(WAIT_OBJECT_0 == wait_result)
        {
            break;
        }
    }
    return 0;
} 
  • 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-30T20:46:45+00:00Added an answer on May 30, 2026 at 8:46 pm

    Yes, this code is correct.

    This example is useful to see how events work. However, there is no need to go reinventing the wheel when you are writing production code. For example, the interaction you described can be elegantly modeled using concurrent data structures like the ones in the Intel TBB library e.g. concurrent_bounded_queue.

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

Sidebar

Related Questions

first time posting here. I'm using a loop to create 3 buttons, but my
first time poster, but please help. I have been searching and reconfiguring for 6
first time using Asp.Net MVC here. I have a model class defined with 3
First time user of stackoverflow, but I have followed its development over on Coding
First time using RewriteRule, so I made RewriteRule ^details/(.*)/(.*)$ /details.php?id=$1&name=$2 [NC] but there are
First time stackoverflow user but occasional lurker, hope you guys can help me out.
First time using JSF, and I'm sort of confused about how getRowData() works. All
first time SO user :) I know that I can format a number like
first time use JTree. Just wondering is it possible to have more than one
First time Stack Overflow poster. Please bear with me! :) I have a set

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.