LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multi-thread programming concerns

Solved!
Go to solution

Yes, I'm not as experienced as I'd like to be, and this will sound elementary to many, but I want to ask about programming concerns in a multi-threaded application. I understand the concept of using a lock (semaphore) on a variable that will be accessed by more than one thread, but what about a block of code? So, let's say that there is a function called TestMe(int tst) that is called in multiple threads. Let's say that one thread calls the function, and in the middle of processing the function's code, the same function is called by a second thread. Is there a danger in this? I realize that the code is just a series of instructions, and I'm guessing that the set of instructions exist only once the application's code. The variables created in the function are created on the stack, so when the function is called in the second thread, its variables would be created on the stack of the second thread and therefore be separate from those created by the first thread, correct?.  The only danger I see is where global variables are used by the function, which would be managed by the semaphore, or a case of static variables declared in the function. Is a static variable created in the thread space or in the global space? Am I thinking on the right track, or am I off-base?

Thanks!

0 Kudos
Message 1 of 6
(4,068 Views)
Solution
Accepted by LelandDurrette

Hi Leland,

 

 

 

Static variables have extent over the entire run of the program. It is only instantiated once and its value will be retained. Therefore, if you have multiple threads accessing the function that has the static variable, you will need to implement a semaphore. This might be helpful: http://quiz.geeksforgeeks.org/static-variables-in-c/

 

I hope this helps!

T. Le
Vision Product Support Engineer
National Instruments
Message 2 of 6
(4,031 Views)
Solution
Accepted by LelandDurrette

Hi Leland

 

The safest way to do multy threaded app is using CRITICAL section provided by the windows SDK

as follows:

declare global var

CRITICAL_SECTION cs ;

In the Main function or your init func:

InitializeCriticalSection(&cs) ;

 

in the cleanup func:

DeleteCriticalSection(&cs) ;

 

where you want to do some code in a thread or read/write a variable

 EnterCriticalSection(&cs) ;
 a = b + c ;

do some code..... 
LeaveCriticalSection(&cs) ;

 

every time your code gets to EnterCriticalSection all other threads are waiting in this point

so use the enter/leave combination with care

for me the code works in an app with 24 threads.

hope its helps

 

 

 

0 Kudos
Message 3 of 6
(4,021 Views)
Solution
Accepted by LelandDurrette

CVI offers Thread Local Variables to perform such tasks: TLVs hold a separate value for each thread that uses them without need to manually add semaphores or critical sections in the code.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 6
(4,011 Views)

I appreciate your time. After posting my question I discovered information on the difference between thread safety and re-entrancy. I understand thread safety pretty well, but don't recall hearing the term "re-entrancy". That is what my question was about.

0 Kudos
Message 5 of 6
(3,971 Views)

I appreciate your time. After posting my question I discovered information on the difference between thread safety and re-entrancy. I understand thread safety pretty well, but don't recall hearing the term "re-entrancy". That is what my question was about.

0 Kudos
Message 6 of 6
(3,968 Views)