WaitForSingleObject Query
-
Hi, I have 2 variants of usage of WaitForSingleObject. Pls tell me which one is the proper usage? Version1:
#include #include HANDLE hEvent;
DWORD WINAPI SampleThread(LPVOID iValue)
{int iFinish = 120; for(int i=100;i<=iFinish;i++) cout<
Version2:
// WaitForSingleObj.cpp : Defines the entry point for the console application. // #include "stdafx.h" // WaitForSingleObject Sample.cpp #include #include DWORD WINAPI SampleThread(LPVOID iValue) { int iFinish = 120; for(int i=100;i<=iFinish;i++) cout<
-
Hi, I have 2 variants of usage of WaitForSingleObject. Pls tell me which one is the proper usage? Version1:
#include #include HANDLE hEvent;
DWORD WINAPI SampleThread(LPVOID iValue)
{int iFinish = 120; for(int i=100;i<=iFinish;i++) cout<
Version2:
// WaitForSingleObj.cpp : Defines the entry point for the console application. // #include "stdafx.h" // WaitForSingleObject Sample.cpp #include #include DWORD WINAPI SampleThread(LPVOID iValue) { int iFinish = 120; for(int i=100;i<=iFinish;i++) cout<
-
Hi, I have 2 variants of usage of WaitForSingleObject. Pls tell me which one is the proper usage? Version1:
#include #include HANDLE hEvent;
DWORD WINAPI SampleThread(LPVOID iValue)
{int iFinish = 120; for(int i=100;i<=iFinish;i++) cout<
Version2:
// WaitForSingleObj.cpp : Defines the entry point for the console application. // #include "stdafx.h" // WaitForSingleObject Sample.cpp #include #include DWORD WINAPI SampleThread(LPVOID iValue) { int iFinish = 120; for(int i=100;i<=iFinish;i++) cout<
It looks as if you want to know when the worker thread has finished, right? Your first alternative waits on an event to be signalled from within the worker thread. Note that as already advised you have to create the event before it is used or you'll be facing a runtime error eventually. By using this technique you will be informed when the thread is about to finish. My point is that by the time the event gets signalled and your waiting function is released, the worker thread may or may not have finished completely. You should wait on the thread handle instead, like you do in your second alternative. The thread handle will enter "signalled" state when the thread has finished completely and has been unloaded from the system. Read more about worker threads here[^].
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
It looks as if you want to know when the worker thread has finished, right? Your first alternative waits on an event to be signalled from within the worker thread. Note that as already advised you have to create the event before it is used or you'll be facing a runtime error eventually. By using this technique you will be informed when the thread is about to finish. My point is that by the time the event gets signalled and your waiting function is released, the worker thread may or may not have finished completely. You should wait on the thread handle instead, like you do in your second alternative. The thread handle will enter "signalled" state when the thread has finished completely and has been unloaded from the system. Read more about worker threads here[^].
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown