`

如何结束线程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++代码

    如何等待一个已有线程自动结束的VC++代码! 值得下载看看!资源免费,大家分享!! 更多免费资源 http://ynsky.download.csdn.net/

    多线程抽签VC++源码实现

     多线程抽签VC++源码实现,若运行时出现“软件目录下不存在列表文件list.txt. 请手动载入或放置到软件目录下”,请将list.txt放置于EXE同级目录内再运行。在这个程序中,抖动线程的结束时间有不确定性,所以不保证...

    Vc++ 打造局域网聊天室

    Vc++ 打造局域网聊天室(9)---程序关闭及线程的结束 http://www.3800hk.com/donghua/g/27574.html Vc++ 打造局域网聊天室(10)---任务栏托盘功能的实现 http://www.3800hk.com/donghua/g/27627.html

    vc++多线程

    包里面有10个文件,分别是:如何安全终止线程;如何创建UI线程;...如何等待线程结束;如何挂起和恢复线程;如何获得线程的退出码;如何使用互斥量;如何使用临界区;如何使用事件;如何使用信号量

    vc++ 应用源码包_3

    内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与文件夹系统操作、系统控制操作、程序...

    10个VC++多线程操作实例源代码

    内容索引:VC/C++源码,系统相关,多线程 10个VC++多线程操作实例源代码,内容涉及如何安全终止线程、如何创建UI线程、如何创建Worker线程、如何等待线程结束、如何挂起和恢复线程、如何获得线程的退出码、如何使用...

    VC++线程开关

    自己做的一个线程开过,希望对于那些不知道如何结束有FOR和WHILE循环的线程的朋友有点帮助。(软件中,线程开关实现线程的开和关,关闭线程实现线程的关闭)

    vc++ 应用源码包_1

    内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与文件夹系统操作、系统控制操作、程序...

    VC++MFC 多线程个种实例

    10.1 如何创建Worker线程 ...10.3 如何等待线程结束 10.4 如何挂起和恢复线程 10.5 如何安全终止线程 10.6 如何获得线程的退出码 10.7 如何使用临界区 10.8 如何使用互斥量 10.9 如何使用信号量 10.10 如何使用事件

    VC++ 中多线程开发学习资料

    多线程的开始、结束、暂停 多线程的交互 理论与实践的总结 有什么问题可以交流:jqb2@163.com

    vc++ 应用源码包_5

    内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与文件夹系统操作、系统控制操作、程序...

    vc++ 应用源码包_2

    内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与文件夹系统操作、系统控制操作、程序...

    vc++ 应用源码包_6

    内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与文件夹系统操作、系统控制操作、程序...

    VC++常用的共用函数100多页

    4. 如何编程结束应用程序?如何编程控制windows的重新引导? 39 5.怎样加载其他的应用程序? 39 6. 确定应用程序的路径 39 7. 获得各种目录信息 40 8. 如何自定义消息 40 9. 如何改变窗口的图标? 40 10. 如何改变窗口的...

    vc++ 开发实例源码包

    内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与文件夹系统操作、系统控制操作、程序...

    VC++ 专家指导.doc

    (26) 如何编程结束应用程序? 23 (27) 如何创建和使用无模式对话框 24 (28) 如何防止主框窗口在其说明中显示活动的文档名 26 (29) 如何在代码中获取工具条和状态条的指针 27 (30) 怎样加载其他的应用程序? ...

    10个VC 多线程操作实例源代码

    10个VC 多线程操作实例源代码,基本上包含了多线程的方方面面,...如何等待线程结束 如何挂起和恢复线程 如何获得线程的退出码 如何使用互斥量 如何使用临界区 如何使用事件 如何使用信号量 有此实例代码,多线程无忧!

    VC++常用功能实例

    4. 如何编程结束应用程序?如何编程控制windows的重新引导? 39 5.怎样加载其他的应用程序? 39 6. 确定应用程序的路径 39 7. 获得各种目录信息 40 8. 如何自定义消息 40 9. 如何改变窗口的图标? 40 10. 如何改变窗口的...

Global site tag (gtag.js) - Google Analytics