I have a situation where, in a multithreaded application, many different threads are accessing a Dictionary at the same time. It appears that this could be a bottleneck, but it’s unclear – a plausible scenario is that multiple threads may be trying to retrieve the same value (do note, however, that the data structure is fixed – no thread is doing any writing, but dozens could be trying to read the same value). The question is, can multiple threads read the same value simultaneously, or is it one at a time? If so, is there any other data structure that could be used?
I have a situation where, in a multithreaded application, many different threads are accessing
Share
Multiple threads should have no problem reading the same memory value. There may be a small wait at the OS or hardware level while the memory is actually accessed for each thread, but that’s miniscule in most cases, and not easy to work around.
What got my attention about your description of the problem is that “dozens [of threads] could be trying to read the same value”. If you have dozens of active threads processing at once, the bottleneck is thread management. Like anything, there is a law of diminishing returns, and diseconomies of scale; with current hardware, at about twice the number of active threads as “execution units” (cores, HT logical processors, however the architecture handles multithreaded execution), your CPU starts spending more time scheduling thread execution and managing thread states than it is actually executing the threaded instructions. Yes, your Task Manager may show hundreds of threads in flight, but the overwhelming majority of these are “sleeping”, listening for user interaction, or just waiting (like polling threads).
I would look at reducing the number of threads to no more than two per “execution unit”, and ideally only a couple more than the number of execution units (so EUs have a thread to “switch to” while the FSB is reading memory for another thread). That will reduce the overhead time your computer spends managing all these threads.