LabWindows/CVI

Content Type
Programming Language
Current manual

Programming with DefineThreadSafeScalarVar

Programming with DefineThreadSafeScalarVar

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 non–array variable.

The DefineThreadSafeScalarVar macro is defined as follows:

DefineThreadSafeScalarVar (datatype, name, 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, 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, if you want to create a thread safe integer named MyCounter, put the following line at global scope in your source file:

DefineThreadSafeScalarVar (int, MyCounter, 0):

The DefineThreadSafeScalarVar macro creates the following accessor functions:

int InitializeMyCounter (void);
void UninitializeMyCounter (void);
int *GetPointerToMyCounter (void);
void ReleasePointerToMyCounter (void);
void SetMyCounter (int val);
int GetMyCounter (void);

If you define the MyCounter variable, make the following function calls:

  • To initialize the thread safe variable, call InitializeMyCounter ();
  • To get the value of the variable from any thread, call int value = GetMyCounter ();
  • To set the value of the variable from any thread, call SetMyCounter (newValue); (where newValue is an integer)
  • To get the pointer to the value from any thread, call int *valuePtr = GetPointerToMyCounter ();
  • To release the pointer before the variable can be accessed from any other thread, call ReleasePointerToMyCounter ();
  • To uninitialize the variable before your program terminates, call UninitializeMyCounter ();

For more information about accessor functions, refer to the Using Macros to Interact with Thread Safe Variables topic.

To create thread safe variables for array data, use the DefineThreadSafeArrayVar macro.

Using a Thread Safe Variable in Multiple Source Files

Use the DeclareThreadSafeScalarVar macro if you want to use the thread safe variable in more than one source file. The DeclareThreadSafeScalarVar macro is defined as follows:

DeclareThreadSafeScalarVar (datatype, name);

where datatype is the data type of the thread safe variable you want to use and name is the name that the thread safe variable you want to use.

Put the following line in a header file that you include in the source files that use the variable:

DeclareThreadSafeScalarVar (int, MyCounter);

DefineThreadSafeScalarVar Example

The following code shows how you would use the MyCounter thread safe variable mentioned in the previous section.

#include <utility.h>

DefineThreadSafeScalarVar (int, MyCounter, 0);
int CVICALLBACK ThreadFunction (void *functionData);

int main (int argc, char *argv[])
{

int functionId;
int *countPtr;
InitializeMyCounter();
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, ThreadFunction, NULL, &functionId);
countPtr = GetPointerToMyCounter();
(*countPtr)++;
ReleasePointerToMyCounter();
CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, functionId, 0);
UninitializeMyCounter();
return 0;

}

int CVICALLBACK ThreadFunction (void *functionData)
{

int *countPtr;
countPtr = GetPointerToMyCounter();
(*countPtr)++;
ReleasePointerToMyCounter();
return 0;

}

Was this information helpful?