| 发表于:2007-09-27 22:19:138楼 得分:0 |
不要使用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)是不起作用的。 | | |
|