|
|
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
|
|
|
|
|
I am voting your replies up.
me too
|
|
|
|
|
granted anyone using Parameters class WILL first check if it was inited or not using getter
|
|
|
|
|
does requirement to have public deinit make harder to make this class thread safe?
|
|
|
|
|
Hi,
I am having a Windows Ui for placing some text in Unighraphics-
So the problem is when i place a text i get "work in process" dialog(which is of Unigraphics), I want to close this but it doesnt,when i close the windows UI this dialog vanishes.
I want Windows ui but i dont want "work in process" dialog.
when i check task manager, i can see two applications , but both say same process in task manager ugraf.exe.
Please help I am using c#
|
|
|
|