NetServerEnum block when thread is terminated externaly

☆樱花仙子☆ 提交于 2019-12-11 13:04:35

问题


(Working in Win32 api , in C environment with VS2010)

I have a two thread app. The first thread forks the second and waits for a given interval - 'TIMEOUT', and then calls TerminateThread() on it. Meanwhile, second thread calls NetServerEnum().

It appears that when timeout is reached , whether NetServerEnum returned successfully or not, the first thread get deadlocked. I've already noticed that NetServerEnum creates worker threads of it's own.

I ultimately end up with one of those threads in deadlock, typically on ntdll.dll!RtlInitializeExceptionChain, unable to exit my process gracefully.


回答1:


As this to too long for a comment:

Verbatim from MSDN, allow me to use te answer form (emphasis by me):

TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:

  • 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.

From reading this it is easy to understanf why it is a bad idea to cancel (terminate) a thread stucking in a system call.


A possible alternative approach to the OP's design might be to spawn off a thread calling NetServerEnum() and simply let it run until the system call returned.

In the mean while the main thread could do other things like for example informing the user that scanning the net takes longer as expected.



来源:https://stackoverflow.com/questions/16950182/netserverenum-block-when-thread-is-terminated-externaly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!