Thread.sleep代码(0):什么是正常的行为? [英] Thread.Sleep(0) : What is the normal behavior?

查看:136
本文介绍了Thread.sleep代码(0):什么是正常的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要我理解的Thread.Sleep(0)强制OS上的上下文切换。

To my understanding a Thread.Sleep(0) force a context switch on the OS.

我要检查什么是可以传递的最大时间量在应用程序之前接收一些CPU时间。

I wanted to check what was the maximum amount of time that could pass in an application before to receive some CPU time.

所以,我建立了一个确实的Thread.Sleep(0)在循环(C#)的应用程序和计算时间每次调用之间传递。

So I built an application that does Thread.Sleep(0) in a while loop (c#) and calculate the time that pass between each call.

在这个应用程序是一个双核心的测试PC的最大观测时间是正确的在1毫秒上运行的唯一一(平均0.9微秒),并使用所有可用的CPU(100%)。

When this application is the only one running on a two core test PC the maximum observed time is right under 1 millisecond (with an average of 0.9 microsecond) and it use all the CPU available (100%).

当我运行它沿着CPU灌装虚拟应用程序(都具有相同的优先级)的最大时间约为25ms的平均时间为20ms。它的行为完全像我期望它。而时间是非常稳定的。

When I run it along a CPU Filling dummy application (all with the same priority) the max time is around 25ms and the average time is 20ms. It behaves exactly like I expect it. And the time is very stable.

每当它得到一些CPU时间立即给予控制权交还给谁有一些处理的事,它就像烫手的山芋游戏(CPU用法下降到0%)。如果世界上没有其他应用程序中运行则控制立即回来。

Whenever it gets some CPU time it immediately give the control back to whoever have some processing to do, it's like the hot potato game (CPU usage drops to 0%). If theres no other application running then the control comes back immediately.

鉴于这种行为我希望这个应用程序在运行现实生活中的应用程序的计算机上的影响微乎其微。 (和给我实际的延迟我可以指望在那里运行的应用程序,看看)。但让我吃惊它没有负面影响(可观察的方式)这一特定系统的性能。

Given this behavior I expected this application to have a minimal impact on a computer running real life application. (And to give me the actual "latency" I could expect to see in the applications running there). But to my surprise it did affect negatively (in an observable way) the performance of this specific system.

我缺少有关的Thread.Sleep一些重要的点(0)?

Am I missing some important point concerning Thread.Sleep(0)?

作为参考这里的这个应用程序

As a reference here's the code of this application

private bool _running = true;
private readonly Stopwatch _timer = new Stopwatch();

private double _maxTime;
private long _count;
private double _average;
private double _current;

public Form1()
{
    InitializeComponent();
    Thread t = new Thread(Run);
    t.Start();
}

public void Run()
{
    while(_running)
    {
        _timer.Start();
        Thread.Sleep(0);
        _timer.Stop();

        _current = _timer.Elapsed.TotalMilliseconds;
        _timer.Reset();
        _count++;

        _average = _average*((_count - 1.0)/_count) + _current*(1.0/_count);
        if(_current>_maxTime)
        {
            _maxTime = _current;
        }
    }
}



编辑为清楚起见(应用程序的目的):
我目前运行的软实时多线程应用程序(当然,应用程序组)需要应对一些投入每一个大约300毫秒,但我们错过一些从时间期限时间(小于的时间为1%)和我目前正在努力提高这一数字。

Edited for clarity (purpose of the application): I am currently running a soft real-time multi-threaded application (well, group of applications) that needs to react to some inputs every roughly 300ms but we do miss some deadlines from time to time (less then 1% of the time) and I'm currently trying to improve that number.

我想确认是什么导致当前的变异在同一台机器上的其他流程:我艰难的,通过拟合这个半实时机器上面写的应用程序中观察到的最大时间会告诉我什么样的变化是由系统引起的。 I.E.我有300毫秒,但是最大观察时间线程得到一些CPU时间是站在50毫秒,所以提高我要我的处理时间设置为最大的250毫秒前的表现(因为我可能已经是50ms的晚期)。

I wanted to verify what is the current variability caused by other process on the same machine: I tough that by fitting the application written above on this semi real-time machine the maximum time observed would tell me what variability is caused by the system. I.E. I have 300ms but max observed time before a thread gets some CPU time is standing at 50ms, so to improve the performance I should set my processing time to a maximum of 250ms (since I might already be 50ms late).

推荐答案

它没有的的上下文切换,只能睡(1)做到这一点。但是,如果有从准备运行任何程序任何其他线程,并且具有更高的优先级,然后睡眠(0),将产生的处理器,并让它运行。您可以通过运行一个无限循环调用睡眠(0),它会燃烧在一个核心100%的CPU周期看到这一点。我不明白你为什么不遵守这一行为。

It doesn't force a context switch, only Sleep(1) does that. But if there's any other thread from any process ready to run and has a higher priority then Sleep(0) will yield the processor and let it run. You can see this by running an endless loop that calls Sleep(0), it will burn 100% CPU cycles on one core. I don't understand why you don't observe this behavior.

要保持系统响应的最佳方式是通过给你的线程优先级低。

The best way to keep the system responsive is by giving your thread a low priority.

这篇关于Thread.sleep代码(0):什么是正常的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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