I have a static class with several static methods.
private static Session _session = new Session();
public static void Method1() {
if(_session != null)
_session.Action();
}
public static void Method2() {
if(_session != null)
_session.Action();
}
public static void Method3() {
if(_session != null)
_session.Action();
}
public static void Method4(string path) {
_session.Disconnect();
_session.Connect(new Config(path));
}
Method1, Method2, Method3 are completely thread safe, they can be safely called simultaneously from any number of threads. In fact, for performance reasons, I need to allow multiple threads to call method1,2 at the same time.
The problem is that Method1,2,3 can throw an exception when Method4 () is called. How to allow several threads to call Method1,2,3, and also block them when calling method4 ()?
source
share