在特定时间段内阻止线程的精确方法是什么?

发布于 2024-08-02 15:57:13 字数 308 浏览 2 评论 0原文

我正在开发的系统需要使用 IEnumerable 工作项,迭代每个工作项,并在它们之间等待一段时间。我希望枚举站点的系统尽可能简单。也就是说,我想要一个可以在 foreach 块末尾调用的方法,该方法将阻塞我指定的特定时间量 - 但我不想使用 Thread.Sleep 因为它无法保证精确。

我需要阻塞的最短时间是 100 毫秒,但我不希望它阻塞的时间超过这个时间,我有时会看到 Thread.Sleep 这样做(我假设是因为上下文中所花费的时间)开关等)。

编辑:相关;使用超时值调用 WaitHandle.Wait 是否与 Thread.Sleep 执行相同的操作?

The system I'm working on needs to consume an IEnumerable of work items, iterate through each of them, and in between them wait for a certain period of time. I would like to keep the system as simple as possible at the enumeration site. That is, I'd like to have a method that I can call at the end of the foreach block which will block for the specific amount of time I specify - but I don't want to use Thread.Sleep because it's impossible to guarantee precision.

The minimum amount of time I'll need to block for is 100ms, but I don't want it to block any longer than this, which I've seen Thread.Sleep do on occasion (I assume because of the time taken in context switches, etc).

Edit: Related; does a call to WaitHandle.Wait with a timeout value do the same thing as Thread.Sleep?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2024-08-09 15:57:13

Thread.Sleep() 的精度约为 20 毫秒,或多或少。如果不使用(非托管)多媒体计时器,您将不会得到任何更好的结果。因为你只是暂停了一点点,这似乎有点过分了。尝试睡眠(80)。

另外:据我所知,所有超时值都具有与 Sleep() 相同的分辨率。

Thread.Sleep() has an accuracy of approx 20ms, give or take. You won't be getting anything better without going to (unmanaged) multimedia timers. And since you are just pausing a liitle that seems overkill. Try Sleep(80).

Additional: all timeout values have, as far as I know, the same resolution as Sleep().

逆光飞翔i 2024-08-09 15:57:13

为什么枚举时需要等待?

如果 IEnumerable 集合在您处理它时发生更改,您将得到一个异常,因此事实上,当您的线程正在处理它时,其他线程不能添加或删除任何项目。

既然如此,为什么要人为地拖延呢?只需在它到来时使用它,然后让调度程序在线程之间分配工作。

如果您想要一个非常非常精确的等待时间,我建议您使用 Thread.Sleep(time - 20 ms),然后忙于等待正确的时间来完成您的工作。

只有实时操作系统才能给你这样的精度。您可以假设 Thread.Sleep 的精度约为 20 毫秒,因此理论上您可以休眠直到所需时间 - 实际时间约为 20 毫秒,然后旋转 20 毫秒,但您将不得不浪费这 20 毫秒。即使这并不能保证你会得到实时结果,调度程序可能只是在你的线程即将执行相关部分时(在旋转之后)将其取出

Why exactly do you need to wait while enumarating?

If a IEnumerable collection changes while you're going through it you'll get an exception so as a matter of fact no items can be added or removed by other threads while you thread is working on it.

Given that, why the artificial delay? Just consume it as it comes and let the scheduler distribute the work among your threads.

If you want a really really precise wait time I suggest you use a Thread.Sleep(time - 20 ms) and then busy wait for the right time do your work.

Only real time operating system can give you such precision. You can assume Thread.Sleep has a precision of about 20 ms so you could, in theory sleep until the desired time - the actual time is about 20 ms and THEN spin for 20 ms but you'll have to waste those 20 ms. And even that doesn't guarantee that you'll get real time results, the scheduler might just take your thread out just when it was about to execute the RELEVANT part (just after spinning)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文