Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance counters and threading

Tags:

c#

I am creating some custom performance counters. I will be creating tasks on a thread pool and incrementing/decrementing the counters from within multiple worker threads.

Do I need to give each thread a new counter object? Is it safe to share a performance counter object cross-thread (for increment/decrement)

like image 886
JMarsch Avatar asked Jan 13 '10 23:01

JMarsch


1 Answers

The PerformanceCounter class already uses a threadsafe wrapper, an internal class named SharedPerformanceCounter. It uses Interlocked.Increment() to increment a counter value for example.

There's no need to lock yourself.

like image 61
Hans Passant Avatar answered Sep 23 '22 02:09

Hans Passant