Local static variable is thread safe?
-
I have such a function: template T& GetInst() { static T t; return t; } I'm not sure whether this is thread safe? If T's constructor has many operations, when two thread entered this function early and later, is there such scenario: the first thread is in T's constructor's, the second thread define t again?:)
-
I have such a function: template T& GetInst() { static T t; return t; } I'm not sure whether this is thread safe? If T's constructor has many operations, when two thread entered this function early and later, is there such scenario: the first thread is in T's constructor's, the second thread define t again?:)
-
I have such a function: template T& GetInst() { static T t; return t; } I'm not sure whether this is thread safe? If T's constructor has many operations, when two thread entered this function early and later, is there such scenario: the first thread is in T's constructor's, the second thread define t again?:)
Hi, Since the value of the locat static variable is stored on heap, per my opinion it should not be thread safe.
Manoj Never Gives up
-
I have such a function: template T& GetInst() { static T t; return t; } I'm not sure whether this is thread safe? If T's constructor has many operations, when two thread entered this function early and later, is there such scenario: the first thread is in T's constructor's, the second thread define t again?:)
I`m REMY wrote:
the second thread define t again?
I think this depends upon the Template Parameter. If first thread invokes the function with class1 type and second invokes with class2 type then those two will be entering different constructors. Since template will generate 2 copies of template class, one for class1 and other for class2.
-
I`m REMY wrote:
the second thread define t again?
I think this depends upon the Template Parameter. If first thread invokes the function with class1 type and second invokes with class2 type then those two will be entering different constructors. Since template will generate 2 copies of template class, one for class1 and other for class2.
I don't means different class. For example: class A { public: A(){...//some operations} }; void main() { Create_Thread(foo()); Crteae_Thread(foo()); } Is this safe?:)
-
I don't means different class. For example: class A { public: A(){...//some operations} }; void main() { Create_Thread(foo()); Crteae_Thread(foo()); } Is this safe?:)
If the function foo() have a local static variable then its not thread safe.
Manoj Never Gives up
-
If the function foo() have a local static variable then its not thread safe.
Manoj Never Gives up
So, how to make it safe? Use thread mutex: ? A& foo() { Wait_Mutex(...); static A a; Release_Mutex(...); } ?? This looks very bad performance!:)
-
So, how to make it safe? Use thread mutex: ? A& foo() { Wait_Mutex(...); static A a; Release_Mutex(...); } ?? This looks very bad performance!:)
-
It depends on what you need. If you wish to have it shared for all threads, you need to work with it like with any other shared memory in multithreading which includes synchronisation (unless only one thread is writing the variable)
rrrado
Of course, I want to do a shared function by all thread. However, I hope to only have one synchronization in this function. I means, the first thread entered the synchronize, all following thread return the reference of object directly.