04-07-2023
02:05 AM
- last edited on
03-28-2025
09:12 AM
by
Content Cleaner
Hello everyone, maybe it's a simple question but i don't get it... usually when i do multithread application i create local variable like this
/// HIFN Document your function here.
/// HIFN You may use multiple lines for documentation.
/// HIRET Document return value here.
/// HIRET You may use multiple lines for documentation.
/// HIPAR functionData/Document parameter here.
int CVICALLBACK ThreadFunction(void *functionData)
{
int MyLocalVariable = {0};
while(quit)
{
// do something
}
}
and every thread have their MyLocalVariable that can contain different value, so why NI suggest to do this?
int CVICALLBACK ThreadFunction (void *functionData);
int tlvHandle;
int gSecondaryThreadTlvVal;
int main (int argc, char *argv[])
{
int functionId;
int *tlvPtr;
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
CmtNewThreadLocalVar (sizeof(int), NULL, NULL, NULL, &tlvHandle);
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, ThreadFunction, 0, &functionId);
CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, functionId, 0);
CmtGetThreadLocalVar (tlvHandle, &tlvPtr);
(*tlvPtr)++;
// Assert that tlvPtr has been incremented only once in this thread.
assert (*tlvPtr == gSecondaryThreadTlvVal);
CmtDiscardThreadLocalVar (tlvHandle);
return 0;
}
int CVICALLBACK ThreadFunction (void *functionData)
{
int *tlvPtr;
CmtGetThreadLocalVar (tlvHandle, &tlvPtr);
(*tlvPtr)++;
gSecondaryThreadTlvVal = *tlvPtr;
return 0;
}
the only advantage that I see is that also the main thread can have it's value.
maybe it's because of the limit as explained here?
"Although the Windows API provides a mechanism for creating and accessing thread-local variables, this mechanism limits the number of thread-local variables you can have in each process. The LabWindows/CVI Utility Library thread-local variable functions do not have this limitation. The following code demonstrates how to create and access a thread-local variable that holds an integer."
How much is the Windows API limit?
Thanks
Solved! Go to Solution.
04-07-2023 02:37 AM
Hi Holly,
I'll start by saying there are 2 other more standard ways to define thread-local variables:
static __thread char TimeStr[80]="";
and
thread_local static char TimeStr[80]="";
Depending on you C99/C11 compiler.
Yes, declaring a normal variable inside a function will make it local to each thread, but what if you need it static ? For instance to remember a value between calls, or to return a string ? You can do this very simply with thread local variables without complex semaphore logic. The drawback is that you cannot share the values between the threads, but then you use a normal static for that.
04-07-2023 02:44 AM
Actually you hit the only problem that i have, and I didn't think that I can solve with this.
I have a function that i want to return a string, i have to create always a char array variable and pass it as a parameter to get it returned.
04-07-2023 04:34 AM
Si, e facile fare cosi:
char* SomeFunc(int arg) {
static __thread char TimeStr[80]="";
...
return TimeStr;
}
It works well for many things... but you can't use it for this, it'll fail:
printf("%s %s", SomeFunc(1), SomeFunc(10));