Trying to design thread safe class
-
Why are you so much against Deinit function? Is it so problematic? Assume I want Deinit function, I basically gave all the details in above comments and in the main question - would you say in such case my Parameters class is thread safe?
1. About deinit()- if that's what you want, fine. It's just that to me it seems to just bring extra complexity - but hey, it's your code :) 2. Yes, it's thread-safe Best, John
-- LogWizard - Log Viewing can be a joy!
-
1. About deinit()- if that's what you want, fine. It's just that to me it seems to just bring extra complexity - but hey, it's your code :) 2. Yes, it's thread-safe Best, John
-- LogWizard - Log Viewing can be a joy!
Is it also thread safe if I remove isInited checks from X1 and X2 getters? - Granted no one calls any methods of this class, unless a init method is called (this I can ensure because I am using this class directly) - I have isInited check first in any method calling this class.
-
Is it also thread safe if I remove isInited checks from X1 and X2 getters? - Granted no one calls any methods of this class, unless a init method is called (this I can ensure because I am using this class directly) - I have isInited check first in any method calling this class.
1. Yes it is - variables X1/X2 are automatically initialized to zero. 2. You modified your code:
public static int X1()
{
if(!isInited()) throw new Exception("init first");
return x1;}
should be
public static int X1()
{
lock(this) {
if(!isInited()) throw new Exception("init first");
return x1;
}
}Best, John
-- LogWizard - Log Viewing can be a joy!
-
1. Yes it is - variables X1/X2 are automatically initialized to zero. 2. You modified your code:
public static int X1()
{
if(!isInited()) throw new Exception("init first");
return x1;}
should be
public static int X1()
{
lock(this) {
if(!isInited()) throw new Exception("init first");
return x1;
}
}Best, John
-- LogWizard - Log Viewing can be a joy!
Yeah but I said what if I do
public static int X1()
{return x1;
}
Granted none calls these methods before INIT method is called (I can ensure this because I am writing those methods) - I will check
isInited
calls in the start of each method calling this class -
Yeah but I said what if I do
public static int X1()
{return x1;
}
Granted none calls these methods before INIT method is called (I can ensure this because I am writing those methods) - I will check
isInited
calls in the start of each method calling this classNo, that is not thread-safe. This is :
public static int X1{} { lock(locker) return x1; }
Best, John
-
No, that is not thread-safe. This is :
public static int X1{} { lock(locker) return x1; }
Best, John
-
Please look at my final version and tell me what you think: http://codepad.org/T3D6uabs[^]
yes, it's threadsafe
-- LogWizard - Log Viewing can be a joy!
-
yes, it's threadsafe
-- LogWizard - Log Viewing can be a joy!
Thanks a lot, few more questions, and I would appreciate if you can help: - if I add more variables other than x1 and x2 but I maintain same style in accessing and initializing them will it affect thread safety? will it affect performance? (probably). Do you know more performance friendly way to solve the problem my class solves?
-
Thanks a lot, few more questions, and I would appreciate if you can help: - if I add more variables other than x1 and x2 but I maintain same style in accessing and initializing them will it affect thread safety? will it affect performance? (probably). Do you know more performance friendly way to solve the problem my class solves?
1. if you add more variables in the same manner, yes 2. will it affect performance? don't worry about that, performance will be the same. And on the init()/deinit() - the "decrease" won't be noticeable. 3. Your class is fine Best, John
-- LogWizard - Log Viewing can be a joy!
-
1. if you add more variables in the same manner, yes 2. will it affect performance? don't worry about that, performance will be the same. And on the init()/deinit() - the "decrease" won't be noticeable. 3. Your class is fine Best, John
-- LogWizard - Log Viewing can be a joy!
-
1. if you add more variables in the same manner, yes 2. will it affect performance? don't worry about that, performance will be the same. And on the init()/deinit() - the "decrease" won't be noticeable. 3. Your class is fine Best, John
-- LogWizard - Log Viewing can be a joy!
Great job supporting this OP, John. I am voting your replies up. cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
-
Thanks a lot!!! So you are saying if I add more properties in same manner, it won't be problem?
Yup, that's exactly what I'm saying :) Best, John
-- LogWizard - Log Viewing can be a joy!
-
Great job supporting this OP, John. I am voting your replies up. cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
Thanks :-O
-- LogWizard - Log Viewing can be a joy!
-
Yup, that's exactly what I'm saying :) Best, John
-- LogWizard - Log Viewing can be a joy!
-
Great job supporting this OP, John. I am voting your replies up. cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
-
you're welcome Best, John
-- LogWizard - Log Viewing can be a joy!