|
Member 12061600 wrote: 2) I need a deinit method - users should be able to clear the Parameter class object, and set new properties if desired, again using method like init for example - since properties I have deliberately made read only
No you don't. At least, your deinit() does not need to be public anyway. When your init() method is called, you know if you're already initialized (thus, deinit() first, then re-init).
Member 12061600 wrote: 3) Can you show me sample how to achieve this using singleton?
I'm sure you can figure it out on your own. That's how you learn
Best,
John
|
|
|
|
|
yeah but users of this class might need deinit to reinitialize the class
|
|
|
|
|
I will reiterate:
When your init() method is called, inside your init() function, you know if you're already initialized (thus, deinit() first, then re-init).
Best,
John
|
|
|
|
|
deinit has to be public
otherwise if someone calls init twice it will reinitialize automatically on second call - if I follow that approach you mention, is it thread safe?
|
|
|
|
|
|
because that is how the class was used before that in C++ I am rewriting now
Ok if I follow your suggestion then calling init twice - will reinitialize it yes?
But is it thread safe now?
|
|
|
|
|
You really need to get a grasp on threading concepts.
1. Since you're accessing data inside a lock(), it is thread-safe
2. You can force the clients of your class to call init before using it - either with Debug.Asserts, or by throwing an exception if the class is not initalized
3. Why would you want to re-init this several times?
Best,
John
|
|
|
|
|
Few points:
1) I will make that class actually internal and only other class in DLL
will be able to use it (I am writing that DLL too) - users can now use other public classes of DLL which use my Parameters class
2) So the only direct user of that class is ME and I can ensure before
its properties are accessed by other methods in other classes, there is a call checking if the method is inited or not
3) Maybe users want to specify other parameters - hence need for deinit
What do you think now?
|
|
|
|
|
3. you can provide a function (thread-safe) that changes the values (without de-initializing).
Best,
John
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
No, that is not thread-safe. This is :
public static int X1{} { lock(locker) return x1; }
Best,
John
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
you're welcome
Best,
John
|
|
|
|
|
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
|
|
|
|