最浪费时间的CPU效率最高的方法是什么?在线程? [英] What's the most CPU-efficient way to "waste time" in a thread?

查看:212
本文介绍了最浪费时间的CPU效率最高的方法是什么?在线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些线程(100的)每次执行几秒钟。当它们执行时,他们花费大量的时间等待来自另一个系统(串行设备)的响应。我注意到,一次执行100个线程可能是一个资源管理器,所以我实际上限制了可以在任何一个时间启动的线程数。



它发生在我身上尽管在线程内部等待外部事件必须有好的和坏的方法。这个方法是CPU密集型吗?:

  send command; 
重复
直到响应到达;
进程响应;

并且这种方法是否会使其更有效率:

 发送命令; 
重复
睡眠(20);
直到回应到达;
进程响应;

*附加信息*



环境是x86 Windows XP。线程代码是与串行设备的长期相关的一系列交互,但一般来说,它包括将字符写入COM端口(使用AsyncFree串行库),并等待通过驻留字符缓冲区驻留返回的字符,处理它们到达时。我想象串行库使设备读写。线程中的时间可以长达一分钟,或短短几秒钟,但是大部分时间花在等待字符离开端口,或等待响应字符(波特率很慢)因此我的问题是线程在等待时行为的最佳方式。目前,我正在等待 CharactersInBuffer 的循环中调用 Sleep 变为非零,处理每个字符到达时,并在完成回复后退出线程。所以代码看起来更像(忽略超时处理等):

  send命令; 
Packet ='';
重复

重复
睡眠(20);
直到响应字符到达;
构建数据包

直到完成数据包到达
进程响应;


解决方案

如果线程真的在等待像WaitForSingleObject ,它不使用处理器时间,然后超时,没有理由在睡眠中延迟线程。



您的用户没有等待线程回应,不使用处理器时间,其他线程不会被阻止,所以没有理由让线程睡觉。



正如David Heffernan所说在他的评论中,如果现在没有使用100%的CPU,那么没有问题。



如果你是单线程,你可能会使用sleep()偶尔会在等待串口响应之间回复用户。



此外,线程睡眠不会使其更有效率。这将简单地为其他线程产生处理器周期。



看看$ code> sleep(0)作为CPU高效在线程中浪费时间的方式。


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 from another system (a serial device). I am mindful that having 100 threads executing at once could be a resource hog so I actually limit the number of threads that can start at any one time.

It occurs to me though that there must be good and bad ways of waiting for an external event inside a thread. Is this approach CPU-intensive?:

send command ;
repeat
until response arrived ;
process response ;    

and does this approach make it more efficient?:

send command ;
repeat
    Sleep (20) ;
until response arrived ;
process response ;  

* ADDITIONAL INFO *

The environment is x86 Windows XP. The thread code is a long and involved series of interactions with a serial device but in general, it consists of writing characters to a COM port (using the AsyncFree serial library) and waiting for characters to be returned by camping on the incoming characters buffer and processing them when they arrive. I imagine the serial library makes device reads and writes. The time in the thread can be as long as a minute , or as short as a couple of seconds, but most of that time is spent waiting for characters to leave the port, or waiting for the response characters (baud rate is slow), hence my question about the best way for the thread to behave while it is waiting. Currently I am calling Sleep in a loop waiting for CharactersInBuffer to become non-zero, processing each character when it arrives, and exiting the thread when I have the complete response. So the code looks more like (ignoring handling of timeouts, etc):

send command ;
Packet = '' ;
repeat

    repeat
        Sleep (20) ;
    until response character arrived ;
    build Packet

until complete packet arrived
process response ;  

解决方案

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.

这篇关于最浪费时间的CPU效率最高的方法是什么?在线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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