开发者

Thread Exit Events - C++

开发者 https://www.devze.com 2023-02-04 05:48 出处:网络
When using an event (created with ::CreateEvent(...)) to signal a thread to exit, is it necessary to reset that event (::ResetEvent(...)) prior to exiting the thread or closing the event handle (::Clo

When using an event (created with ::CreateEvent(...)) to signal a thread to exit, is it necessary to reset that event (::ResetEvent(...)) prior to exiting the thread or closing the event handle (::CloseHandle(...))? Or, is it good practice, if there is a possibility that the thread will be re-started? This is fo开发者_C百科r a Windows environment. Thanks.


It is not necessary to call reset event, since when you close the handle the event associated with that will no longer be valid.


It depends on the semantics of the event you use.

If you are using the event to trigger more than one thread to die, then you should not reset the event in each thread. If you use it to trigger only one thread to die, then it again depends on further semantics. If the event is used to signal other things to other threads, then you need to reset the event in the dieing thread. Otherwise, if the event is used only to signal a single thread to die, and nothing else, then you can reset the event or not. Nothing else will wake up as a result of the event being signaled.

You dont have to reset an event in order to prevent resource leaks. All you have to do is CloseHandle()


There is no need to flip the set state of the event before closing its handle. To further explain, the set state of the event is unrelated to its existence. As you are likely aware, the CreateEvent(...) function allows you to set the initial state of the event. So if you initialize it as set, should you be sure it set before CloseHandle()? No of course not, the set state is completely inconsequential. It is akin to resetting the members of a class to their starting state before destructing it.

0

精彩评论

暂无评论...
验证码 换一张
取 消