Nik*_*hil 3 c++ multithreading
我使用ZThreads来说明问题,但我的问题适用于C++中的PThreads,Boost Threads和其他类似的线程库.
class MyClass: public Runnable
{
public:
void run()
{
while(1)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
我现在推出如下:
MyClass *myClass = new MyClass();
Thread t1(myClass);
Run Code Online (Sandbox Code Playgroud)
现在是否可以杀死(必要时暴力)这个线程?我可以肯定这样做,而不是我有一个无限循环Thread::Sleep(100000),如果它是阻塞.但我可以杀死旋转的线程(进行计算).如果有,怎么样?如果没有,为什么不呢?
Windows(从MSDN):
TerminateThread是一个危险的函数,只能在最极端的情况下使用.只有在确切知道目标线程正在执行的操作时,才应调用TerminateThread,并且控制目标线程在终止时可能正在运行的所有代码.例如,TerminateThread可能会导致以下问题:
Run Code Online (Sandbox Code Playgroud)If the target thread owns a critical section, the critical section will not be released. If the target thread is allocating memory from the heap, the heap lock will not be released. If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent. If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL.
Boost肯定没有线程杀死功能.
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |