当线程终止externaly块NetServerEnum [英] NetServerEnum block when thread is terminated externaly

查看:179
本文介绍了当线程终止externaly块NetServerEnum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(在Win32 API的工作,在C环境VS2010)

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

我有两个线程的应用程序。第一个线程叉第二,并等待一个给定的时间间隔 - 超时,然后调用 TerminateThread()就可以了。
与此同时,第二个线程调用 NetServerEnum()

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().

看来,达到超时时,无论 NetServerEnum 成功与否返回,第一个线程得到陷入僵局。
我已经注意到了 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.

我最终结束了死锁这些线程之一,通常在 ntdll.dll中!RtlInitializeExceptionChain ,无法正常退出我的过程。

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

推荐答案

至于这太长评论:

逐字从 MSDN ,允许我使用TE问答形式(由我强调):

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

TerminateThread是应该只有在最极端的情况下可以使用一个危险的功能。你应该调用TerminateThread只有当你确切地知道目标线程是干什么的,和控制所有code的目标线程也可能会被运行的在终止时间。例如,TerminateThread可导致下列问题:

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:


      
  • 如果目标线程拥有临界区,临界区将不会被释放。

  •   
  • 如果目标线程是从堆中分配内存,堆锁将不会被释放。
      *如果目标线程正在执行某些KERNEL32呼叫时终止,KERNEL32的状态线程的进程可能会不一致。

  •   
  • 如果目标线程操作的共享DLL的全局状态,DLL的状态可能会被破坏,影响DLL的其他用户。

  •   

从阅读这很容易understanf为什么它是一个糟糕的主意,取消(终止)一个线程stucking在系统调用。

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

一个可能的替代办法到OP的设计可能是产卵了一个线程调用 NetServerEnum(),只是让它运行,直到系统调用返回。

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.

这篇关于当线程终止externaly块NetServerEnum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆