LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

protecting static locals in multi-threaded applications

Hi, I am new to multi-threading and have a few questions.

I have read the NI info on multi-threading but need some clarity on static locals. Am I right in saying that instances of the same function in different threads that use static locals actually access the same memory location? Is this why they need protecting?

Also, should I use the individual thread-safe variable functions when protecting statics, rather than the NI macros as these only seem to work at global scope.

Finally, is it true that statics in multi-thread apps are probably of very limited use and static local type functionality would be better achieved using thread local variables instead?

Thanks

Jamie Fraser
0 Kudos
Message 1 of 4
(2,806 Views)
The problem with variable shared by different Thread is in fact how the Compiler optimise the code.
You can have a situation where the variable is compiled in such a way that the code will access it via the cache, instead of accessing it thought its RAM location, and therefore one instance will have a totally different value from another one...

To avoid this situation there is a "C" and "C++" keyword which tells the compiler to not optimise the location of the variable. This keyword is "volatile".

In other word, whenever a variable (int, double, struct, static,....) must be accessed from more than one thread, add the volatile keyword to its declaration.

examples:
typedef struct
{
int val1;
int val2;
}myStruct_DEF;


volatile int myShar
edValue;
volatile myStruct_DEF mysharedStruct;

Note 1:
when you declare a structure volatile, all its members are automatically set to volatile.

Note 2:
You might get some casting error while accessing some volatile variable, in this case add a cast of the original type:
Example:
volatile int mySharedValue;
.....
int MyshareIntValue;
MyshareIntValue=(int)mySharedValue;
....


Concerning your 2nd question, you can declare volatile static variable without any problems...
static volatile int MyStaticSharedValue.

For the 3rd comments, I wouldn't say of "very limited use", that depends on the program....

Best regards.
0 Kudos
Message 2 of 4
(2,806 Views)
Thanks for the reply.

"Concerning your 2nd question, you can declare volatile static variable without any problems...
static volatile int MyStaticSharedValue"

Does this mean that static locals can be adequately protected just by using the volatile qualifier to prevent caching, or do I still need to use the thread-safe functions. If I DO need to use the thread safe functions, can I use the macros, or do I need to use the functions directly?

Jamie
0 Kudos
Message 3 of 4
(2,806 Views)
Hi jamie,
The "static volatile" will only make sure that:
1. The variable will not be cached, so any access to it will force the CPU to lookup/change its content in the RAM and not the cache. (volatile)
2. The variable will have the same content for all the thread (static).

If you do need a lock mecanism (mutex) on the variable you will have to use one of the variable thread safe functions.
This really depends on the program you are developing, the role of the variable and how it is used.

Best Regards,
Farid.
0 Kudos
Message 4 of 4
(2,806 Views)