Is Stopwatch.ElapsedTicks threadsafe?
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)
From MSDN:
Any instance members are not guaranteed to be thread safe.
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.
Looking at the source code, it is not thread-safe.