What's the most CPU-efficient way to “waste time” in a thread?

前端 未结 3 867
遥遥无期
遥遥无期 2021-02-20 06:08

I have a number of threads (100\'s) that each execute for a few seconds at a time. When they are executing, they spend a significant amount of that time waiting for a response

相关标签:
3条回答
  • 2021-02-20 06:29

    The most efficient way to prevent a thread from using CPU time is to put it in a "wait mode."

    I don't use delphi at all, but it seems that the fundamentals for that are there. See "Chapter 11. Synchronizers and Events" and more specifically "Event simulation using semaphores".

    If you want to wait without using CPU, then use WaitForEvent:

    The signal state of the event is examined. If it indicates that the event is signalled, then the internal semaphore is signalled, and the count of threads blocked on the semaphore is decremented. The count of blocked threads is then incremented, and a wait is performed on the internal semaphore.

    If this is I/O related, then things work a little different. If it's a socket, then it might already be blocking, if it's asynchronous I/O, then you can use a semaphore and WaitForEvent and so on.

    In .NET there is the Monitor.Wait, Monitor.Signal, ManualResetEvent, CountDownLatch, etc., but I don't know what are the equivalent things in delphi.

    0 讨论(0)
  • 2021-02-20 06:43

    If the thread is truly waiting with something like a WaitForSingleObject, which uses no processor time, then times out, there is no reason to put a delay in the thread with sleep.

    Your user isn't waiting on the thread to be responsive, it's not using processor time, and other threads won't be blocked, so there would be no reason to put the thread to sleep.

    As David Heffernan indicated in his comment, if it's not using 100% of your CPU now, then there's no problem.

    You might use sleep() if you were single threaded and you had to occasionally respond to the user in between waiting on the serial port to respond.

    Also, having a thread sleep would not make it more efficient. It would simply yield processor cycles to other threads.

    Take a look at sleep(0) as a CPU efficient way of "wasting time" in a thread.

    0 讨论(0)
  • 2021-02-20 06:49

    I cannot speak for AsyncFree's capabilities, but in general COM port programming in Windows supports Overlapped I/O, so you can efficiently wait for a notification when data arrives by using the WaitCommEvent() function with one of the WaitFor...() family of functions, such as WaitForSingleObject(). The thread can be put into a sleep state until the notify is issues, at which time it "wakes up" to read from the port until there is nothing further to read, then it can go back to sleep until the next notify.

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