Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TugboatEng on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

C win32 threads

Status
Not open for further replies.

aunlu

Computer
Joined
Nov 3, 2003
Messages
3
Location
TR
I am creating two threads, one is waiting for commands at the command line and the other is checking a UDP socket. Whichever happens first, I want to kill the other thread. For example, if user enters something from the commandline, the other thread waiting for UDP packets should exit.
I am using MS visual studio.
How can I do that?
 
The boolean would work if the thread is looping around and not actually waiting.
I would do a WaitForMultipleObjects instead of the WaitForSingleObject in you waiting threads and create the special mutex or a semaphore to wait for so that the thread that receives input first can set the boolean flag and then signal this mutex to unblock the other thread.

You also need to think through the race conditions.
BUT here is another idea - using the same WaitForMultipleObjects can try to wait for input from both sources in the same thread - you are not actually doing anything simultaneous anyway. And once the wait returns - you can check which event unblocked it and perform the appropriate whatever. that way no need for two threads. Unless of course you have other reasons to use 2 threads.
HTH.
 
Sounds you have at least threads running. Once either thread you create gets input, it posts a message to your primary thread (using ::PostThreadMessage) asking your primary thread to terminate the other one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top