如何结束线程VC++
Terminating a Thread
Terminating a thread has the following results:
结束线程产生以下结果:
Any resources owned by the thread, such as windows and hooks, are freed. 线程的资源被释放
The thread exit code is set.设置线程退出码
The thread object is signaled. 标记线程对象
If the thread is the only active thread in the process, the process is terminated. For more information, see Terminating a Process.
进程只有一个线程,则进程被停止,参看停止进程
The GetExitCodeThread function returns the termination status of a thread. While a thread is executing, its termination status is STILL_ACTIVE. When a thread terminates, its termination status changes from STILL_ACTIVE to the exit code of the thread.
获取退出线程代码函数返回线程的退出状态,当线程执行时候,结束状态为STILL_ACTIVE.当线程被结束,结束状态为退出线程的退出码
When a thread terminates, the state of the thread object changes to signaled, releasing any other threads that had been waiting for the thread to terminate. For more about synchronization, see Synchronizing Execution of Multiple Threads.
当线程停止时,标记线程对象的状态,启动其他在等待的线程。关于“同步”请看“多线程同步机制”
When a thread terminates, its thread object is not freed until all open handles to the thread are closed.
当一个线程停止,它的线程对象没有释放,直达关于该线程的句柄被关闭
How Threads are Terminated
如何结束线程
A thread executes until one of the following events occurs:
有以下方法
The thread calls the ExitThread function.
线程自己调用ExitThread函数
Any thread of the process calls the ExitProcess function.
进程里面的任何线程调用 ExitProcess 函数
The thread function returns.
线程函数自己返回
Any thread calls the TerminateThread function with a handle to the thread.
拥有该线程句柄的其他线程调用TerminateThread 函数
Any thread calls the TerminateProcess function with a handle to the process.
拥有该线程句柄的其他线程调用TerminateProcess 函数
The exit code for a thread is either the value specified in the call to ExitThread, ExitProcess, TerminateThread, or TerminateProcess, or the value returned by the thread function.
线程的退出码是调用ExitThread,ExitProcess,TerminateThread或者TerminateProcess的特殊值,或者是线程函数返回的值。
If a thread is terminated by ExitThread, the system calls the entry-point function of each attached DLL with a value indicating that the thread is detaching from the DLL (unless you call the DisableThreadLibraryCalls function). If a thread is terminated by ExitProcess, the DLL entry-point functions are invoked once, to indicate that the process is detaching. DLLs are not notified when a thread is terminated by TerminateThread or TerminateProcess. For more information about DLLs, see Dynamic-Link Libraries.
如果线程函数是用ExitThread函数结束的,系统用调用和线程相关的每一个dll的入口点函数(如果没有调用函数DisableThreadLibraryCalls )。如果线程是用ExitProcess函数结束的,dll的入口点函数被回调一次,用于表明和进程分离了。DLL不会被通知,当进程是用TerminateThread或 TerminateProcess函数结束的。
The TerminateThread and TerminateProcess functions should be used only in extreme circumstances, since they do not allow threads to clean up, do not notify attached DLLs, and do not free the initial stack. In addition, handles to objects owned by the thread are not closed until the process terminates. The following steps provide a better solution:
函数TerminateThread 和TerminateProcess 只在特殊情况下使用,因为他们不允许线程清理,不通知dll,不是放初始化的堆栈。并且,线程的对象句柄不会被关闭知道进程结束。以下方法提供比较好的解决方法
Create an event object using the CreateEvent function.
用函数CreateEvent函数创建事件对象
Create the threads.
创建线程
Each thread monitors the event state by calling the WaitForSingleObject function.
每一个线程调用函数WaitForSingleObject监控事件状态
Use a wait time-out interval of zero.
Each thread terminates its own execution when the event is set to the signaled state (WaitForSingleObject returns WAIT_OBJECT_0).
每一个进程结束它自己的执行当事件设置为信号状态(WaitForSingleObject)
补充:软件开发 , Vc ,