Programming with DefineThreadSafeArrayVar
- Updated2023-02-21
- 2 minute(s) read
Programming with DefineThreadSafeArrayVar
The DefineThreadSafeScalarVar macro defines a thread safe variable handle and type–safe wrapper functions, or accessor functions, that you call to access a thread safe variable. Use the DefineThreadSafeScalarVar macro at global scope in a source file to define your array variable.
The DefineThreadSafeScalarVar macro is defined as follows:
DefineThreadSafeArrayVar(datatype, name, numElements, maxGetPointerNesting);
where datatype is the data type of the thread safe variable you want to create, name is the name that the macro uses to create customized accessor function names for the thread safe variable, numElements specifies the number of elements in the array, and maxGetPointerNesting is the maximum number of nested calls to the GetPointerTo accessor functionmaximum number of nested calls to the GetPointerTo accessor function.
For example, to create a thread safe array of 10 integers named MyData, put the following line at global scope in your source file:
DefineThreadSafeArrayVar (int, Array, 10, 0);
The DefineThreadSafeArrayVar macro creates the following accessor functions:
int InitializeMyData (void);
void UninitializeMyData (void);
int *GetPointerToMyData (void);
void ReleasePointerToMyData (void);
If you define the MyData variable, make the following function calls:
- To initialize the thread safe variable, call InitializeMyData ();
- To get the pointer to the value from any thread, call int *valuePtr = GetPointerToMyData ();
- To release the pointer before the variable can be accessed from any other thread, call ReleasePointerToMyData ();
- To uninitialize the variable before your program terminates, call UninitializeMyData ();
Note that, unlike DefineThreadSafeScalarVar, DefineThreadSafeArrayVar does not define the Get and Set accessor functions. Use the GetPointer accessor function to interact with array data.
For more information about accessor functions, refer to the Using Macros to Interact with Thread Safe Variables topic.
To create thread safe variables for non-array data, use the DefineThreadSafeScalarVar macro.
Using a Thread Safe Array in Multiple Source Files
Use the DeclareThreadSafeArrayVar macro if you want to use the thread safe array in more than one source file. The DeclareThreadSafeArrayVar macro is defined as follows:
DeclareThreadSafeArrayVar (datatype, name);
where datatype is the data type of the thread safe array you want to use and name is the name that the thread safe array you want to use.
Put the following line in a header file that you include in the source files that use the array:
DeclareThreadSafeArrayVar (int, MyData);
DefineThreadSafeArrayVar Example
The following code shows how you would use the MyData thread safe variable mentioned in the previous section.
#include <ansi_c.h>
#include <utility.h>
#define arraySize 3
DefineThreadSafeArrayVar (int, MyData, arraySize, 0);
int CVICALLBACK ThreadFunction (void *functionData);
int main (int argc, char *argv[])
{
int functionId;
int *dataPtr;
int index = 0;
int value = 0;
InitializeMyData();
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, ThreadFunction, NULL, &functionId);
dataPtr = GetPointerToMyData();
printf("Main:\t\tValues:\t");
for (index=0; index<arraySize; index++)
{
value = *(dataPtr + index)+1;
*(dataPtr + index) = index*10 + value;
printf("%d\t", *(dataPtr + index));
}
printf("\n");
ReleasePointerToMyData();
CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, functionId, 0);
UninitializeMyData();
return 0;
}
int CVICALLBACK ThreadFunction (void *functionData)
{
int *dataPtr;
int index = 0;
int value = 0;
dataPtr = GetPointerToMyData();
printf("ThreadFunction:\tValues:\t");
for (index=0; index<arraySize; index++)
{
value = *(dataPtr + index)+1;
*(dataPtr + index) = index*10 + value;
*(dataPtr + index) = value++;
printf("%d\t", *(dataPtr + index));
}
printf("\n");
ReleasePointerToMyData();
return 0;
}