Call Reentrant Code in a Multithreaded Environment - MATLAB & Simulink (original) (raw)
Main Content
To call reentrant code, create a main
function that:
- Includes the header file
_primaryfunctionname_
.h. - For each thread, allocates memory for the global memory allocation structure
_primaryfunctionname_
StackData
. - If the algorithm uses persistent or global data, allocates memory for the global structure
_primaryfunctionname_
PersistentData
. If the threads communicate, allocate this memory once for the root process. Otherwise, you can allocate memory per thread. - Contains a thread function that calls these functions:
_primaryfunctionname_
_initialize
._primaryfunctionname_
._primaryfunctionname_
_terminate
.
When you convert a MATLAB® function to a C/C++ library function or a C/C++ executable,MATLAB Coder™ generates two housekeeping functions. Call these functions in the code that calls the generated C/C++ function. For more information, seeDeploy Generated Code.
- Initializes each thread and passes in a pointer to the memory allocation structure as the first parameter to the thread function.
- Frees up the memory used for global structures.