Software Archive
Read-only legacy content
17061 Discussions

WaitForSingleObject

tkibedi
Novice
552 Views
I am writing a mufti-thread program and try coordinate the execution of the main process and a thread using the CreateEvent, SetEvent and WaitForSingleObject Win32 routines. Both, the main process and the thread, set their own event, when they are ready to receive data from the other. The thread, created by the main process should wait indefinetily.
To achieve this I try to use the

WaitForSingleObjecth(Event, WAIT_INFINITE)

According to the CVF Programmer`I Guide I should use the DFMT and DFWIN modules, but it causes several compiler errors, as the same routines (CREATETHREAD, WAITFORSINGLEOBJECT, etc) are declared in both. I decided to use the DFWIN routine and declare the WAIT_INFINITE integer parameter (=-1).

Inteerestingly, the WAITFORSINGLEOBJECT routine in the thread returns only at evry second time, when the object (hEvent) has been signaled in the main process. Using the Debugger, setting breaks at the

SETEVENT(hEvent)

and the

WaitForSingleObjecth(hEvent, WAIT_INFINITE)

result returns for every single event. Setting breaks only one of the above, result "abnormal" return. It seems to me there might be some timing problem?

To overcome the above problem the thread waits only for a definite time and if it times out, the WaitForSingleObject routine is called again and again in an infinite loop untill the hEvent object is signaled by the main process.
0 Kudos
1 Reply
Jugoslav_Dujic
Valued Contributor II
552 Views
DFMT is primarily intended for use in apps which require multithreading only, not full Win32 interfaces. DFWIN contains DFMT and a lot more. The constant in question should be INFINITE (accorging to docs, which is defined in DFWINTY), not WAIT_INFINITE, but its value is -1, so it should work.

Back to essential problem, to me it looks like the order of happening is not quite
OK. Have in mind that threads are not 100% parallel on a single-processor machine -- they are executed in chunks of code one, then another. The situation you have (WaitForSingleObject succeeds every second time) might be result of
something like:

 
---SetEvent-->|                                                |--ResetEvent-->...| 
              |---WFSO abandoned----WFSO again entered&leaved--|                  |-WFSO entered&waits 


Or something similar. You might consider using automatic-reset Events. If you already do... well, I don't know.

HTH
Jugoslav
0 Kudos
Reply