简体   繁体   English

性能计数器和线程

[英]Performance counters and threading

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) 共享性能计数器对象跨线程是否安全(增量/减量)

The PerformanceCounter class already uses a threadsafe wrapper, an internal class named SharedPerformanceCounter. PerformanceCounter类已经使用了线程安全包装器,一个名为SharedPerformanceCounter的内部类。 It uses Interlocked.Increment() to increment a counter value for example. 例如,它使用Interlocked.Increment()来递增计数器值。

There's no need to lock yourself. 没有必要锁定自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM