I'm using a lock
over a section of code. Although the critical section is small, many threads would reach there (about 100 threads per second). In some articles I see this method is not suitable for heavy threading environments
.
My question is at what rate we c开发者_如何学Call an environment heavy threaded
?
Unfortunately, I don't think there's any specific, numerical definition of heavy threaded. Some quick Googling showed me recent context switch benchmark numbers quoting context-switching costs as somewhere between 1us and 30us. Pessimistically assuming 30us, this means:
(30 us/switch * 100 switch/sec) == 3ms/sec == 0.3% context-switch overhead
for doing the 100 context switches per second you mention. No worry there, but context switches aren't the whole story: you'll also have to consider the costs of mutex contention, which I'd recommend measuring in your own application and with the specific mutex implementation you're using.
The bottom line is that if you get acceptable throughput for your app, it's fine to use this pattern at these rates. My guess is that mutex contention is likely to dominate, and thus that the running time (and variability of running time) of your critical section could make the difference between this working for you and not working for you.
精彩评论