02-05-2017 03:18 PM
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!
Solved! Go to Solution.
02-06-2017 10:44 AM
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!
02-07-2017 01:46 AM
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
02-07-2017 07:25 AM
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.
02-13-2017 07:50 PM
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.
02-13-2017 07:51 PM
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.