暂停一个线程不到一毫秒 [英] Pause a thread for less than one millisecond

查看:248
本文介绍了暂停一个线程不到一毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在消息客户端测试应用程序中,生产者线程需要被限制以避免淹没服务器。



由于传输速率约为每秒25,000条消息(40每个消息的微秒),Sleep(1)造成的延迟太长了。



睡眠小于一毫秒包含与Windows API相关的一些信息。有没有Delphi的代码片段,类或库?






以下Bens答案我发现Sleep的值不同于15还提供不同的传输速率(Windows Vista):



每20封消息后睡眠(1):

  00:02 tx / rx 25740/3705 12831/1846 msgs / sec(77/541微秒/ msg)
00:04 tx / rx 53101/7405 13255/1848 msgs / sec (75/541微秒/ msg)
00:06 tx / rx 79640/11288 13260/1879 msgs / sec(75/532微秒/ msg)
00:08 tx / rx 104520/14562 13055 / 1818 msgs / sec(76/550 microsecs / msg)
00:10 tx / rx 130760/18829 13066/1881 msgs / sec(76/531 microsecs / msg)
每20封邮件后,睡眠(5):$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ > 00:02 tx / rx 7640/3622 3812/1807 msgs / sec(262/553 microsecs / msg)
00:04 tx / rx 14660/10794 3661/2695 msgs / sec(273/371 microsecs / msg )
00:06 tx / rx 21480/18171 3577/3026 msgs / sec(279/330 microsecs / msg)
00:08 tx / rx 28140/25642 3515/3203 msgs / sec (284/312微秒/ msg)
00:10 tx / rx 34980/32692 3496/3267 msgs / sec(286/306 microsecs / msg)

在阅读关于忙碌等待下限的评论后,这是意想不到的。



而不调节的值



  00:02 tx / rx 44065/494 21988/246 msgs / sec(45/4065 microsecs / msg)
00 :04 tx / rx 90493/756 22595/188 msgs / sec(44/5319 microsecs / msg)
00:06 tx / rx 142982/907 23810/151 msgs / sec(41/6622 microsecs / msg)
00:08 tx / rx 192562/1144 24055/142 msgs / sec(41/7042 microsecs / msg)
00:10 tx / rx 237294/1395 23717/139 msgs / sec(42/7194 microsecs / msg)


解决方案

发送20条消息,然后睡1毫秒?



除了系统定时器之外,除了有硬件中断之外,您不能睡眠少于调度器量程。阅读您链接的问题的答案,他们解释为什么建议的方法实际上不起作用。



即使使用这种方法,您可能睡眠时间超过1ms,或者消息不会立即发送,整个操作一定要花费超过1ms,降低整体速率。



因此,每次唤醒时都要检查一个精确的时钟源,根据经过的时间计算要发送的消息数量,不要使用常数20。


In a messaging client test application, the producer thread needs to be throttled to avoid flooding the server.

As the transfer rates are around 25,000 messages per second (40 microseconds per message), the delay caused by Sleep(1) would far too long.

Sleep Less Than One Millisecond contains some information related to the Windows API. Is there a code snippet, class or library for Delphi?


Following Bens answer I found that Sleep with different values below 15 also gives different transfer rates (Windows Vista):

Sleep(1) after every 20 messages:

00:02 tx/rx 25740/3705 12831/1846 msgs/sec (77/541 microsecs/msg)
00:04 tx/rx 53101/7405 13255/1848 msgs/sec (75/541 microsecs/msg)
00:06 tx/rx 79640/11288 13260/1879 msgs/sec (75/532 microsecs/msg)
00:08 tx/rx 104520/14562 13055/1818 msgs/sec (76/550 microsecs/msg)
00:10 tx/rx 130760/18829 13066/1881 msgs/sec (76/531 microsecs/msg)

Sleep(5) after every 20 messages:

00:02 tx/rx 7640/3622 3812/1807 msgs/sec (262/553 microsecs/msg)
00:04 tx/rx 14660/10794 3661/2695 msgs/sec (273/371 microsecs/msg)
00:06 tx/rx 21480/18171 3577/3026 msgs/sec (279/330 microsecs/msg)
00:08 tx/rx 28140/25642 3515/3203 msgs/sec (284/312 microsecs/msg)
00:10 tx/rx 34980/32692 3496/3267 msgs/sec (286/306 microsecs/msg)

This was unexpected after reading the comment about the lower limit for busy waiting

And the values without throttling

00:02 tx/rx 44065/494 21988/246 msgs/sec (45/4065 microsecs/msg)
00:04 tx/rx 90493/756 22595/188 msgs/sec (44/5319 microsecs/msg)
00:06 tx/rx 142982/907 23810/151 msgs/sec (41/6622 microsecs/msg)
00:08 tx/rx 192562/1144 24055/142 msgs/sec (41/7042 microsecs/msg)
00:10 tx/rx 237294/1395 23717/139 msgs/sec (42/7194 microsecs/msg)

解决方案

Send 20 messages and then sleep for 1 millisecond?

You can't sleep for less than the scheduler quantum, unless you have a hardware interrupt other than the system timer. Read the answers in the question you linked, they explain why the suggested approach doesn't actually work.

Even with this approach you might sleep longer than 1ms, or since the messages aren't sent instantaneously, the whole operation will surely take longer than 1ms, reducing the overall rate.

So, inspect a precision clock source every time you wake, and calculate how many messages to send based on elapsed time, don't use a constant 20.

这篇关于暂停一个线程不到一毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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