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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing thread 1

Status
Not open for further replies.

mingis

Programmer
Jan 23, 2002
475
LT
Should be threads closed explicitly (by sending WM_QUIT message and a ExitThread() as a responce to it, for example)? Are not all child threads closed authomatically by ending of the main parent process? What to do in case, when child thread Sleep()'s or accept()'s sockets? How to wake thread up or let him go out from the accept() state?
 
Maybe you should number your questions

1) Should be threads closed explicitly (by sending WM_QUIT message and a ExitThread() as a responce to it, for example)?
I'd say yes

2) Are not all child threads closed authomatically by ending of the main parent process?
No - you can force it by using _exit. That will kill everything but it doesn't flush all the buffers first.

3) What to do in case, when child thread Sleep()'s or accept()'s sockets? How to wake thread up or let him go out from the accept() state?
You can send a message to yourself, wait for multiple objects or just _exit.
 
Thank you for responce.

You can send a message to yourself

Do you mean send message to the thread? But when it is in Sleep() or accept() state it does not accept any messages.

wait for multiple objects

In case of accept() you could never get listen socket be signaled when no incomming IP requests are received.
 
If the thread is in Sleep, there is a nice way of waking it up: APCs (Asynchronous Procedure Calls). How does it works (quote from MSDN):

An asynchronous procedure call (APC) is a function that executes asynchronously in the context of a particular thread. When an APC is queued to a thread, the system issues a software interrupt. The next time the thread is scheduled, it will run the APC function. An APC generated by the system is called a kernel-mode APC. An APC generated by an application is called a user-mode APC. A thread must be in an alertable state to run a user-mode APC.


Each thread has its own APC queue. An application queues an APC to a thread by calling the QueueUserAPC function. The calling thread specifies the address of an APC function in the call to QueueUserAPC. The queuing of an APC is a request for the thread to call the APC function.

When a user-mode APC is queued, the thread to which it is queued is not directed to call the APC function unless it is in an alertable state. A thread enters an alertable state when it calls the SleepEx, SignalObjectAndWait, MsgWaitForMultipleObjectsEx, WaitForMultipleObjectsEx, or WaitForSingleObjectEx function.

Link:

A simpler approach is to create a non-signaled event which is passed to the thread. Every time you must "Sleep", you call WaitForSingleObject on that event with the desired interval. Because the event is not signaled the WaitForSingleObject will exit with timeout (WAIT_TIMEOUT) thus correctly behaving as a Sleep call. But, when you want to quickly wake up a thread you can do a SetEvent (or a PulseEvent) and the WaitForSingleObject will exit with WAIT_OBJECT_0 (object was signaled).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top