Stopwatch.ElapsedTicks 线程安全吗?

发布于 2024-11-19 18:28:59 字数 280 浏览 2 评论 0原文

如果我有一个共享的 System.Diagnostics.Stopwatch 实例,多个线程能否以安全的方式调用 shared.ElapsedTicks 并获得准确的结果?

以这种方式使用 Stopwatch 的共享实例与使用静态 GetTimeStamp() 方法之间在线程安全/准确性方面有什么区别吗?

我正在测量大约 180 毫秒的间隔,发现使用共享实例给我带来了更大范围的结果,其中包括大量比我预期短的结果。

该机器有多个 CPU(2 个 Intel X5550)

If I have a shared System.Diagnostics.Stopwatch instance, can multiple threads call shared.ElapsedTicks in a safe manner and get accurate results?

Is there any difference in terms of thread-safety/accuracy between using a shared instance of Stopwatch in this way, and using the static GetTimeStamp() method?

I'm measuring intervals of around 180ms, and finding that using the shared instance is giving me a larger spread of results, including a significant number that are shorter than I would expect.

The machine has multiple CPUs (2 * Intel X5550 for what it's worth)

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

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

发布评论

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

评论(4

记忆で 2024-11-26 18:28:59

来自 MSDN

不保证任何实例成员都是线程安全的。

From MSDN:

Any instance members are not guaranteed to be thread safe.

长途伴 2024-11-26 18:28:59

查看源代码 是线程安全的,但不能使用:Stop()Reset()Restart()

因此,如果您启动一个共享实例,不修改它并仅调用 ElapsedXXX 属性,那么应该没问题。

Looking at the source code, it is thread-safe, but you must not use: Stop(), Reset() and Restart().

So, if you start a shared instance, does not modify it and call only ElapsedXXX properties, you should be fine.

有深☉意 2024-11-26 18:28:59

看源码,不是线程安全的。

Looking at the source code, it is not thread-safe.

A君 2024-11-26 18:28:59

您可以使用 https://msdn.microsoft。 com/en-us/library/dd642243(v=vs.110).aspx

ThreadLocal<T> 

像这样:

    ThreadLocal<Random> _localRandom = new ThreadLocal<Random>(() => new Random());
    ThreadLocal<Stopwatch> _localStopwatch = new ThreadLocal<Stopwatch>(() => new Stopwatch());

    public void SomeTest()
    {
        Action someAction = () =>
            {
                _localStopwatch.Value.Reset();
                _localStopwatch.Value.Start();
                Thread.Sleep(_localRandom.Value.Next(100, 500));
                _localStopwatch.Value.Stop();
                Debug.Print(_localStopwatch.Value.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
            };

        var actions = Enumerable.Range(0, 1000).Select(i => someAction).ToArray();
        Parallel.Invoke(new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount}, actions);
    }

You can use https://msdn.microsoft.com/en-us/library/dd642243(v=vs.110).aspx

ThreadLocal<T> 

like this:

    ThreadLocal<Random> _localRandom = new ThreadLocal<Random>(() => new Random());
    ThreadLocal<Stopwatch> _localStopwatch = new ThreadLocal<Stopwatch>(() => new Stopwatch());

    public void SomeTest()
    {
        Action someAction = () =>
            {
                _localStopwatch.Value.Reset();
                _localStopwatch.Value.Start();
                Thread.Sleep(_localRandom.Value.Next(100, 500));
                _localStopwatch.Value.Stop();
                Debug.Print(_localStopwatch.Value.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
            };

        var actions = Enumerable.Range(0, 1000).Select(i => someAction).ToArray();
        Parallel.Invoke(new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount}, actions);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文