SyncLock on SyncRoot
multithreading, synclock, vb.net
Solution
Yes, that's exactly what the `SyncRoot` property is for. In particular, it means that if another "wrapper" collection is created, it will probably use the same `SyncRoot`, so everyone can still synchronize correctly.
You could create your own lock object, and that would make absolutely sure that no-one else locked at the same time - but it would also mean that other code couldn't safely work with the same collection. It really depends on the scenario - is other code able to use the same collection? Is that other code under your control?
Note that you should take out a lock for all operations, not just Enqueue/Dequeue. In particular, if you want to iterate over the collection you should hold the lock for that whole time, as a change in the middle will invalidate the iterator.
Problem
I have created a synchronized queue and am using SyncLock on the SyncRoot property of that queue when I invoke the Enqueue/Dequeue methods. The methods are invoked from instances of standard producer/consumer classes. Is that a proper use of the SyncRoot property? Would it be better practice to create a private shared object in each class and lock on that instead? Please explain your reasoning.