Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to set event in case of calling CreateSemaphore ? I mean what I should use to avoid Main from exiting before my threads if I am implying CreateSemaphore in the Main itself??? I am using QueueUserWorkItem with Semaphore. Anybody please.
Posted
Updated 18-Feb-12 5:37am
v2

1 solution

If you need to prevent main from exiting before your worker threads, then a semaphore really has nothing to do with that. You have to wait for your threads to finish. Do this by using the WaitForMultipleObjects() call with your thread handles.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025%28v=vs.85%29.aspx[^]
 
Share this answer
 
v3
Comments
Abhineet Ayan Verma 20-Feb-12 0:02am    
I am using QueueUserWorkItem to create multiple threads, so cant use WaitForMultipleObjects as it needs an array of Handles in its second parameter.
Albert Holguin 20-Feb-12 1:19am    
That's .net, is your application .net? Please tag accordingly.
Abhineet Ayan Verma 20-Feb-12 1:21am    
No, my application is not .Net. Its WinAPI Win32 C.
Albert Holguin 20-Feb-12 1:55am    
Oh ok (.net version is the first thing that comes up on MSDN)... Well, use a synchronization event and pass the event handle to the thread, when he's done, he can signal the event. Main can simply wait for the event (or events). See CreateEvent(), ResetEvent(), and SetEvent().
Abhineet Ayan Verma 20-Feb-12 1:58am    
Thanks Albert Holguin, I am already using the CreateEvent Method but was stuck in the logic as to when call the SetEvent as I am having a thread pool. I got the solution by using InterlockedIncrement and InterlockedDecrement. Thanks anyways for your efforts.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900