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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:04:17+00:00 2026-06-13T16:04:17+00:00

#include <windows.h> #include <iostream> using namespace std; int count = 0; DWORD WINAPI Tf1

  • 0
#include <windows.h>
#include <iostream>
using namespace std;

int count = 0;

DWORD WINAPI Tf1 ( LPVOID n )
{
    HANDLE hEvent = OpenEvent ( EVENT_ALL_ACCESS , false, (LPCWSTR)"MyEvent" );
    if ( !hEvent ) { return -1; }
    // Loop through and wait for an event to occur
    for ( ;; )
    {
        // Wait for the Event
        WaitForSingleObject ( hEvent, INFINITE );
        count++;
        printf("In function1, Counter value: %d\n",count);
        //    No need to Reset the event as its become non signaled as soon as
        //    some thread catches the event.
        if( count > 7 ) return 0;
    }
    CloseHandle(hEvent);
    return 0;
}

DWORD WINAPI Tf2 ( LPVOID n )
{

    HANDLE hEvent = OpenEvent ( EVENT_MODIFY_STATE , false, (LPCWSTR)"MyEvent" );
    if ( !hEvent ) return -1; 

    for ( ;; )
    {
        if( count % 2 == 0) 
            SetEvent (hEvent);
        else
        {
            count++;
            printf("In function2, Counter value: %d\n",count);
        }
        //    No need to Reset the event as its become non signaled as soon as
        //    some thread catches the event.
        if( count > 7 ) return 0;
    }
    CloseHandle(hEvent);
    return 0;
}

int main()
{
    //    Create an Auto Reset Event which automatically reset to 
    //    Non Signalled state after being signalled
    HANDLE     hEvent = CreateEvent ( NULL , false , false , (LPCWSTR)"MyEvent" );
    if ( !hEvent ) return -1;
    //    Create a Thread Which will wait for the events to occur
    HANDLE mutex = CreateMutex(NULL, FALSE, NULL);

    DWORD Id;
    HANDLE hThrd1 = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE)Tf1,0,0,&Id );
    HANDLE hThrd2 = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE)Tf2,0,0,&Id );

    if ( !hThrd1 ) { CloseHandle (hEvent); return -1; }
    if ( !hThrd2 ) { CloseHandle (hEvent); return -1; }
    // Wait for a while before continuing....
    Sleep ( 1000 );
    // Give the signal twice as the thread is waiting for 2 signals to occur

    // Wait for the Thread to Die
    WaitForSingleObject ( hThrd1, INFINITE );
    WaitForSingleObject ( hThrd2, INFINITE );

    CloseHandle ( hThrd1 );
    CloseHandle ( hThrd2 );
    CloseHandle ( hEvent );

    system ( "PAUSE" );
    return 0;
}

O/P:

In function1, Counter value: 1
In function1, Counter value: 3
In function2, Counter value: 2
In function2, Counter value: 4
In function2, Counter value: 6
In function1, Counter value: 5
In function1, Counter value: 7
In function2, Counter value: 8

Desired O/P:

In function1, Counter value: 1
In function2, Counter value: 2
In function1, Counter value: 3
In function2, Counter value: 4
In function1, Counter value: 5
In function2, Counter value: 6
In function1, Counter value: 7
In function2, Counter value: 8

I know i am not utilizing the power of threads. But my requirement is like this. How can i achieve this?

  • 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-06-13T16:04:18+00:00Added an answer on June 13, 2026 at 4:04 pm

    There is nothing to stop race conditions in this code. You either need two events so the threads play ping-pong with signal and wait, or you protect your counter with a critical section.

    Option 1:

    // Thread 1
    {
        increment counter
        Signal event 2
        Wait on event 1
    }
    
    // Thread 2
    {
        Wait on event 2
        increment counter
        Signal event 1
    }
    

    Option 2: (using your existing paradigm)

    // Thread 1
    {
        Wait on event
        Acquire mutex
        if count is even
            increment counter
        end
        Release mutex
    }
    
    // Thread 2
    {
        Acquire mutex
        if count is even
            Signal event
        else
            increment counter
        end
        Release mutex
    }
    

    Note that option two is a bit racey in Thread 2… It might spin multiple times while Thread 1 responds to the event. In that respect, you may as well remove the event altogether and have both threads spin out… If you are trying to avoid that, then use my Option 1.

    There are other ways to go, including the use of interlocked increments, but try one of the above anyway.

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

Sidebar

Related Questions

This is my code: #include <Windows.h> #include <ShlObj.h> #include <iostream> using namespace std; int
#include <iostream> #include <windows.h> using namespace std; int main(){ LPWSTR test = LC:/aizen.png; int
here is algorithm #include<iostream> #include<Windows.h> #include<time.h> using namespace std; int main(){ int a[10]={12,3,5,2,7,80,10,1,16,30}; long
#include <iostream> #include <windows.h> using namespace std; int main() { wstring my_str(LEl Niño );
#include <iostream> #include <iomanip> #include <stdio.h> #include <windows.h> #include <process.h> using namespace std; HANDLE
#include <iostream> using namespace std; int main () { cout << Hello World!; return
this code: #include <stdio.h> #include <iostream> using namespace std; int main() { cout <<
I wrote a simple program in c++: #include<iostream> using namespace std; int main() {
#include <iostream> #include <Windows.h> #include <process.h> //#include windowstate.cpp //DWORD WINAPI MyThreadFunction( LPVOID lpParam );
My .cpp program: #include <iostream> using namespace std; int main() { cout << 5;

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.