I was going through a legacy code and found that the code uses SuspendThread Function to suspend the execution of a worker thread. Whenever the worker thread needs to process a request, the calling thread resumes this worker thread. Once the task is done the thread suspends itself.
我正在浏览遗留代码,发现代码使用SuspendThread函数来暂停工作线程的执行。只要工作线程需要处理请求,调用线程就会恢复此工作线程。任务完成后,线程将自行挂起。
I don’t know why it was done this way. According to me it could have been done more elegantly using an Event object with WaitForSingleObject API.
我不知道为什么这样做。根据我的说法,可以使用WaitForSingleObject API的Event对象更优雅地完成它。
My question is, what are the benefits (if any) of suspending a thread as compared to making a thread wait on a synchronization object? In which scenarios would you prefer SuspendThread, ResumeThread APIs?
我的问题是,与使线程等待同步对象相比,暂停线程有什么好处(如果有的话)?在哪种情况下,您更喜欢SuspendThread,ResumeThread API?
2 个解决方案
#1
No.
Suspending a thread is discouraged in every environment I've ever worked in. The main concern is that a thread may be suspended while holding onto a lock on some resource, potentially causing a dead lock. Any resources saved in terms of synchronization objects aren't worth the deadlock risks.
在我曾经工作的每个环境中都不鼓励挂起一个线程。主要关注的是线程可能会在某个资源上锁定时被挂起,可能导致死锁。根据同步对象保存的任何资源都不值得死锁风险。
This is not a concern when a thread is made to wait, as the thread inherently controls its own "suspension" and can be sure to release any locks it is holding.
当一个线程等待时,这不是一个问题,因为线程固有地控制它自己的“暂停”并且可以确保释放它所持有的任何锁。
If you read the documentation on SuspendThread, you'll see that it is meant for use by debuggers. Tear it out of any application code if you can.
如果您阅读有关SuspendThread的文档,您将看到它适用于调试器。如果可以,请将其从任何应用程序代码中删除。
To illustrate my point, a list of the "do not use" suspension methods I've come across:
为了说明我的观点,我遇到了一个“不使用”暂停方法的列表:
As an aside; I'm really surprised that Thread.Suspend in .NET was "supported" in 1.0/1.1, it really should have been warning worthy from the start.
作为旁白;我真的很惊讶.NET中的Thread.Suspend在1.0 / 1.1中被“支持”,它应该从一开始就是值得警告的。
#2
You'll need a separate event object for each thread if you want to be able to wake up a specific thread. That would lead to higher kernel object consumption which is not good by itself and could possibly cause problems on early versions of Windows. With manual resume you don't need any new kernel objects.
如果您希望能够唤醒特定线程,则每个线程都需要一个单独的事件对象。这将导致更高的内核对象消耗,这本身并不好,并且可能在早期版本的Windows上引起问题。使用手动恢复,您不需要任何新的内核对象。
#1
No.
Suspending a thread is discouraged in every environment I've ever worked in. The main concern is that a thread may be suspended while holding onto a lock on some resource, potentially causing a dead lock. Any resources saved in terms of synchronization objects aren't worth the deadlock risks.
在我曾经工作的每个环境中都不鼓励挂起一个线程。主要关注的是线程可能会在某个资源上锁定时被挂起,可能导致死锁。根据同步对象保存的任何资源都不值得死锁风险。
This is not a concern when a thread is made to wait, as the thread inherently controls its own "suspension" and can be sure to release any locks it is holding.
当一个线程等待时,这不是一个问题,因为线程固有地控制它自己的“暂停”并且可以确保释放它所持有的任何锁。
If you read the documentation on SuspendThread, you'll see that it is meant for use by debuggers. Tear it out of any application code if you can.
如果您阅读有关SuspendThread的文档,您将看到它适用于调试器。如果可以,请将其从任何应用程序代码中删除。
To illustrate my point, a list of the "do not use" suspension methods I've come across:
为了说明我的观点,我遇到了一个“不使用”暂停方法的列表:
As an aside; I'm really surprised that Thread.Suspend in .NET was "supported" in 1.0/1.1, it really should have been warning worthy from the start.
作为旁白;我真的很惊讶.NET中的Thread.Suspend在1.0 / 1.1中被“支持”,它应该从一开始就是值得警告的。
#2
You'll need a separate event object for each thread if you want to be able to wake up a specific thread. That would lead to higher kernel object consumption which is not good by itself and could possibly cause problems on early versions of Windows. With manual resume you don't need any new kernel objects.
如果您希望能够唤醒特定线程,则每个线程都需要一个单独的事件对象。这将导致更高的内核对象消耗,这本身并不好,并且可能在早期版本的Windows上引起问题。使用手动恢复,您不需要任何新的内核对象。