Click here to Skip to main content
12,065,888 members (25,753 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VC6 C++
What is the purpose of ResetEvent(), While we can complete the task by using CreateEvent(),SetEvent() & WaitForSingleObject() functions?
Posted 29-Jun-11 19:28pm
Comments
SAKryukov 1-Jul-11 0:22am
   
It looks like you thinks you known why using SetEvent.
In this case, I do not understand, how it's possible to not understand why using ResetEvent.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Google is your friend, use it well.

http://msdn.microsoft.com/en-us/library/ms685081(v=vs.85).aspx[^]

According to MSDN

The ResetEvent function is used primarily for manual-reset event objects, which must be set explicitly to the nonsignaled state. Auto-reset event objects automatically change from signaled to nonsignaled after a single waiting thread is released.
  Permalink  
Comments
SAKryukov 1-Jul-11 0:25am
   
That's correct, my 5.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

ResetEvent() is for 'un'SetEvent()'ing

In your use case you're obviously using the event singularly, but there are cases where the WaitForSingleObject() is within a loop and will be called again, normally in a job workerthread scenario (although PulseEvent() may be more appropriate sometimes)

main thread
// pseudo code
for(int loop=0;looper<10;looper++)
{
// add job
AddJobToWorkerThread();
// signal a new job is to be done
SetEvent(newJobToBeDoneEvent);
// and sleep
Sleep(10000);
}

workerthread
// 
while(!threadToBeTerminated)
{
if(WaitForSingleObject(newJobToBeDoneEvent,5000)==WAIT_OBJECT_0)
{
// do the job we were posted
...
// then clear the flag and wait again
ResetEvent(newJobToBeDoneEvent);
}
else
   break;
}
  Permalink  
Comments
Chuck O'Toole 30-Jun-11 1:21am
   
Microsoft recommends not using PulseEvent().

From MSDN

PulseEvent Function
Sets the specified event object to the signaled state and then resets it to the nonsignaled state after releasing the appropriate number of waiting threads.

Note This function is unreliable and should not be used. It exists mainly for backward compatibility. For more information, see Remarks.
barneyman 30-Jun-11 1:29am
   
you learn something new every day - thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160207.1 | Last Updated 30 Jun 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100