开发者

Waiting thread count and working thread count on Semaphore

Is there any way to find that

  1. ho开发者_运维百科w many threads are waiting on semaphore?
  2. how many threads have currently occupied semaphore?
  3. if i use threadpool thread to wait on semaphore, how to let main thread wait until threadpool thread is finished.

Thanks.


This is forbidden knowledge in thread synchronization. Because it is utterly impossible to ever make this accurate. It represents an unsolvable race condition. When you use Habjan's approach, you'll conclude that there are, say, two threads waiting. A microsecond later another thread calls WaitOne() and there are three. But you'll make decisions based on that stale value.

Race conditions are nothing to mess with, they are incredibly hard to debug. They have a habit of making your code fail only once a week. As soon as you add instrumenting code to try to diagnose the reason your code fails, they'll stop occurring because that added code changed the timing.

Never do this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜