Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you lock on a generic dictionary?

Or should you always create some other lock object?

like image 435
leora Avatar asked Jan 04 '09 01:01

leora


1 Answers

Yes, cast it to an IDictionary and lock on .SyncRoot:

Generic.Dictionary<int, int> dic = new Generic.Dictionary<int, int>();

lock (((IDictionary)dic).SyncRoot)
{
    // code
}

Thanks to this source for the info.

Of course a thread-safe dictionary would be nice, too, as others have suggested.

like image 61
Michael Haren Avatar answered Oct 04 '22 11:10

Michael Haren