Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This is a follow up question to my previous question my previous question. I am trying to design a thread safe parameters class with only readable getters. Idea is it should be possible to initialize this class once with some parameters - and throughout app I should be able to query values. Deinitialization should also be possible.

Please find below. I am interested if this is thread safe or not, or how much thread safe it is?. This is my real code. Other code related feedback also welcome.

static class Parameters
{
 private static int connectionTimeout; // only getters of these will be visible to outside world
 private static int responseTimeout; 
 private static bool isInit;
 private static readonly object Locker = new object();
 
 public static bool isInited()
 {
 lock(Locker)
 {
 return isInit;
 }
 }
 
 public static int ConnectionTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return connectionTimeout;
 }
 
 }
 
 public static int ResponseTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return responseTimeout;
 }
 
 }
 
 public static void init(int ct, int rt)
 {
 lock(Locker)
 {
 if(isInit) throw new Exception ("Already inited");
 isInit = true;
 connectionTimeout= ct;
 responseTimeout= rt; 
 }
 }
 
 public static void Deinit()
 {
 
 lock(Locker)
 {
 if(!isInit) throw new Exception ("Already deinited");
 isInit = false;
 connectionTimeout= 0;
 responseTimeout= 0; 
 }
 }
 
}

This is a follow up question to my previous question. I am trying to design a thread safe parameters class with only readable getters. Idea is it should be possible to initialize this class once with some parameters - and throughout app I should be able to query values. Deinitialization should also be possible.

Please find below. I am interested if this is thread safe or not, or how much thread safe it is?. This is my real code. Other code related feedback also welcome.

static class Parameters
{
 private static int connectionTimeout; // only getters of these will be visible to outside world
 private static int responseTimeout; 
 private static bool isInit;
 private static readonly object Locker = new object();
 
 public static bool isInited()
 {
 lock(Locker)
 {
 return isInit;
 }
 }
 
 public static int ConnectionTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return connectionTimeout;
 }
 
 }
 
 public static int ResponseTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return responseTimeout;
 }
 
 }
 
 public static void init(int ct, int rt)
 {
 lock(Locker)
 {
 if(isInit) throw new Exception ("Already inited");
 isInit = true;
 connectionTimeout= ct;
 responseTimeout= rt; 
 }
 }
 
 public static void Deinit()
 {
 
 lock(Locker)
 {
 if(!isInit) throw new Exception ("Already deinited");
 isInit = false;
 connectionTimeout= 0;
 responseTimeout= 0; 
 }
 }
 
}

This is a follow up question to my previous question. I am trying to design a thread safe parameters class with only readable getters. Idea is it should be possible to initialize this class once with some parameters - and throughout app I should be able to query values. Deinitialization should also be possible.

Please find below. I am interested if this is thread safe or not, or how much thread safe it is?. This is my real code. Other code related feedback also welcome.

static class Parameters
{
 private static int connectionTimeout; // only getters of these will be visible to outside world
 private static int responseTimeout; 
 private static bool isInit;
 private static readonly object Locker = new object();
 
 public static bool isInited()
 {
 lock(Locker)
 {
 return isInit;
 }
 }
 
 public static int ConnectionTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return connectionTimeout;
 }
 
 }
 
 public static int ResponseTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return responseTimeout;
 }
 
 }
 
 public static void init(int ct, int rt)
 {
 lock(Locker)
 {
 if(isInit) throw new Exception ("Already inited");
 isInit = true;
 connectionTimeout= ct;
 responseTimeout= rt; 
 }
 }
 
 public static void Deinit()
 {
 
 lock(Locker)
 {
 if(!isInit) throw new Exception ("Already deinited");
 isInit = false;
 connectionTimeout= 0;
 responseTimeout= 0; 
 }
 }
 
}
edited tags
Link
user87512
user87512
added 137 characters in body
Source Link
user87512
user87512

This is a follow up question to my previous question. I am trying to design a thread safe parameters class with only readable getters. Idea is it should be possible to initialize this class once with some parameters - and throughout app I should be able to query values. Deinitialization should also be possible.

Please find below. I am interested if this is thread safe or not, or how much thread safe it is?. This is my real code. Other code related feedback also welcome.

static class Parameters
{
 private static int x1;connectionTimeout; // only getters of x1 and x2these will be visible to outside world
 private static int x2;responseTimeout; 
 private static bool isInit;
 private static readonly object Locker = new object();
 
 public static bool isInited()
 {
 lock(Locker)
 {
 return isInit;
 }
 }
 
 public static int X1ConnectionTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return x1;connectionTimeout;
 }
 
 }
 
 public static int X2ResponseTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return x2;responseTimeout;
 }
 
 }
 
 public static void init(int xct, int yrt)
 {
 lock(Locker)
 {
 if(isInit) throw new Exception ("Already inited");
 isInit = true;
 x1 =connectionTimeout= x;ct;
 x2 =responseTimeout= y;rt; 
 }
 }
 
 public static void Deinit()
 {
 
 lock(Locker)
 {
 if(!isInit) throw new Exception ("Already deinited");
 isInit = false;
 x1 =connectionTimeout= 0;
 x2 =responseTimeout= 0;
 }
 }
 
}

This is a follow up question to my previous question. I am trying to design a thread safe parameters class with only readable getters. Idea is it should be possible to initialize this class once with some parameters - and throughout app I should be able to query values. Deinitialization should also be possible.

Please find below. I am interested if this is thread safe or not, or how much thread safe it is?. This is my real code. Other code related feedback also welcome.

static class Parameters
{
 private static int x1; // only getters of x1 and x2 will be visible to outside world
 private static int x2; 
 private static bool isInit;
 private static readonly object Locker = new object();
 
 public static bool isInited()
 {
 lock(Locker)
 {
 return isInit;
 }
 }
 
 public static int X1()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return x1;
 }
 
 }
 
 public static int X2()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return x2;
 }
 
 }
 
 public static void init(int x, int y)
 {
 lock(Locker)
 {
 if(isInit) throw new Exception ("Already inited");
 isInit = true;
 x1 = x;
 x2 = y; 
 }
 }
 
 public static void Deinit()
 {
 
 lock(Locker)
 {
 if(!isInit) throw new Exception ("Already deinited");
 isInit = false;
 x1 = 0;
 x2 = 0;
 }
 }
 
}

This is a follow up question to my previous question. I am trying to design a thread safe parameters class with only readable getters. Idea is it should be possible to initialize this class once with some parameters - and throughout app I should be able to query values. Deinitialization should also be possible.

Please find below. I am interested if this is thread safe or not, or how much thread safe it is?. This is my real code. Other code related feedback also welcome.

static class Parameters
{
 private static int connectionTimeout; // only getters of these will be visible to outside world
 private static int responseTimeout; 
 private static bool isInit;
 private static readonly object Locker = new object();
 
 public static bool isInited()
 {
 lock(Locker)
 {
 return isInit;
 }
 }
 
 public static int ConnectionTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return connectionTimeout;
 }
 
 }
 
 public static int ResponseTimeout()
 {
 lock(Locker)
 {
 if(isInit == false) throw new Exception("init first");
 return responseTimeout;
 }
 
 }
 
 public static void init(int ct, int rt)
 {
 lock(Locker)
 {
 if(isInit) throw new Exception ("Already inited");
 isInit = true;
 connectionTimeout= ct;
 responseTimeout= rt; 
 }
 }
 
 public static void Deinit()
 {
 
 lock(Locker)
 {
 if(!isInit) throw new Exception ("Already deinited");
 isInit = false;
 connectionTimeout= 0;
 responseTimeout= 0;
 }
 }
 
}
added 32 characters in body
Source Link
user87512
user87512
Loading
linked the previous question
Source Link
svick
  • 24.5k
  • 4
  • 53
  • 89
Loading
Source Link
user87512
user87512
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /