#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?
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:
Option 2: (using your existing paradigm)
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.