Quantcast
Channel: Best C# solution for multithreaded threadsafe read/write locking? - Stack Overflow
Browsing all 9 articles
Browse latest View live

Answer by JPB for Best C# solution for multithreaded threadsafe read/write...

I thank you all and I'm glad to share this demo program, inspired by the above contributions, that run 3 modes (not safe, mutex, slim).Note that setting "Silent = false" will result in no conflict at...

View Article



Answer by Joseph Kingry for Best C# solution for multithreaded threadsafe...

Small ValuesFor small values (basically any field that can be declared volatile), you can do the following:private static volatile int backingField;public static int Field{ get { return backingField; }...

View Article

Answer by Brian Rasmussen for Best C# solution for multithreaded threadsafe...

Several others have already explained how to use the lock keyword with a private lock object, so I will just add this: Be aware that even if you lock inside each method in your type, calling more than...

View Article

Answer by Jorge Córdoba for Best C# solution for multithreaded threadsafe...

Although you could just use a single mutex to control all the access to the class (effectively serializing the access to the class) I suggest you study the static class, determine which members are...

View Article

Answer by jason for Best C# solution for multithreaded threadsafe read/write...

class LockExample { static object lockObject = new object(); static int _backingField = 17; public static void NeedsLocking() { lock(lockObject) { // threadsafe now } } public static int...

View Article


Answer by Chris Chilvers for Best C# solution for multithreaded threadsafe...

Locking in static methods sounds like a bad idea, for one thing if you use these static methods from class constructor you could run into some interesting side-effects due to loader locks (and the fact...

View Article

Answer by Reed Copsey for Best C# solution for multithreaded threadsafe...

You should lock/unlock on each static member access, within the static accessor, as needed.Keep a private object to use for locking, and lock as required. This keeps the locking as fine-grained as...

View Article

Answer by Andrew Hare for Best C# solution for multithreaded threadsafe...

The safest and shortest way is to create a private, static field of type Object that is only used for locking (think of it as a "pad-lock" object). Use this and only this field to lock on as this...

View Article


Best C# solution for multithreaded threadsafe read/write locking?

What is the safest (and shortest) way do lock read/write access to static members in a multithreaded environment in C#? Is it possible to do the threadsafe locking & unlocking on class level (so I...

View Article

Browsing all 9 articles
Browse latest View live




Latest Images