首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么暂停正在运行的四个线程

2012-03-11 
如何暂停正在运行的四个线程?如何暂停正在运行的四个线程?AAfxBeginThread(ThreadFunc1,this)BAfxBegin

如何暂停正在运行的四个线程?
如何暂停正在运行的四个线程?
A   =   AfxBeginThread(ThreadFunc1,this);    
B   =   AfxBeginThread(ThreadFunc2,this);  
C   =   AfxBeginThread(ThreadFunc3,this);
D   =   AfxBeginThread(ThreadFunc4,this);

[解决办法]
DWORD SuspendThread( HANDLE hThread);
[解决办法]
不要使用SuspendThread!!!

1. 容易造成死锁。例如:线程A运行new操作符分配内存,多线程环境的new操作是要加锁的,如果正在已经加锁但还没有解锁的情况下线程A被Suspend了,那么进程内的其它线程只要一调用new操作符则必死无疑,死锁!!
MSDN中对SuspendThread的Remark:
This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.

2. SuspendThread之类的API都是针对User Mode的,当线程运行在Kernel Mode时,这些操作都是不起作用的。例如线程A正在调用WaitForSingleObject函数等待某个内核对象时,调用SuspendThread(A)是不起作用的。

热点排行