I have multiple threads who all need to write to the same Dictionary.
I have a lock on an object that I maintain in order to make sure only 1 thread is updating the dictionary at once.
My question is as follows. If one thread tries to update the dictionary while another has a lock, will the thread just know to wait? Will it just fail? If threads just wait, I can I avoid too many of them from waiting, is there a way to say a maximum of five theads can queue, the rest just go on?
I have multiple threads who all need to write to the same Dictionary. I
Share
If one thread is holding a lock and a second thread tries to acquire the same lock, the second thread will wait.
Do the threads only write, or do you have read operations as well? If that is the case, you should probably use
ReaderWriterLockSlim(unless you already do). That will allow you to have multiple threads reading in parallel, unless a thread is updating in which case it can get exclusive access to the resource.