Is putting thread on hold optimal?

前端 未结 6 1067
礼貌的吻别
礼貌的吻别 2021-01-05 01:11

Application has an auxiliary thread. This thread is not meant to run all the time, but main process can call it very often.

So, my question is, what is more optimal

相关标签:
6条回答
  • 2021-01-05 01:25

    What do you mean by "suspend"? WaitForSingleObject will suspend the thread, i.e., it will not consume any CPU, until the signal arrives.

    0 讨论(0)
  • 2021-01-05 01:26

    I would assume Andrei doesn't use Delphi to write .NET and therefore Suspend doesn't translate to System.Threading.Thread.Suspend but to SuspendThread Win32 API.

    I would strongly suggest against it. If you suspend the thread in an arbitrary moment, then you don't know what's going to happen (for example, you may suspend the thread in such a state the some shared resource is blocked). If you however already know that the thread is in suspendable state, then simply use WaitForSingleObject (or any other WaitFor call) - it will be equally effective as suspending the thread, i.e. thread will use zero CPU time until it is awaken.

    0 讨论(0)
  • 2021-01-05 01:30

    If it's a worker thread that has units of work given to it externally, it should definitely be using signalling objects as that will ensure it doesn't use CPU needlessly.

    If it has any of its own work to do as well, that's another matter. I wouldn't suspend the thread from another thread (what happens if there's two threads delivering work to it?) - my basic rule is that threads should control their own lifetime with suggestions from other threads. This localizes all control in the thread itself.

    0 讨论(0)
  • 2021-01-05 01:32

    Another option would be the TMonitor introduced in Delphi 2009, which has functions like Wait, Pulse and PulseAll to keep threads inactive when there is nothing to do for them, and notify them as soon as they should continue with their work. It is loosely modeled after the object locks in Java. Like there, Delphi object now have a "lock" field which can be used for thread synchrinozation.

    A blog which gives an example for a threaded queue class can be found at http://delphihaven.wordpress.com/2011/05/04/using-tmonitor-1/

    Unfortunately there was a bug in the TMonitor implementation, which seems to be fixed in XE2

    0 讨论(0)
  • 2021-01-05 01:39

    In terms of CPU resources used, both solutions are the same - the thread which is suspended and thread which is waiting in WaitForSingleObject for an object which is not signalled both get no CPU cycles at all.

    That said, WaitForSingleObject is almost always a prefered solution because the code using it will be much more "natural" - easier to read, and easier to make right. Suspending/Resuming threads can be dangerous, because you need to take a lot of care to make sure you know you are suspending a thread in a state where suspending it will do no harm (imagine suspending a thread which is currently holding a mutex).

    0 讨论(0)
  • 2021-01-05 01:40

    See the excellent tutorial on multi-threading in Delphi : Multi Threading Tutorial

    0 讨论(0)
提交回复
热议问题