Main Content

Generating and Calling Reentrant Code

Generate and call reentrant code for single or multithreaded environments

By default, the generated code creates static variables for variables whose size exceeds the maximum stack usage value, stored in the configuration object setting StackUsageMax. The data in static variables persists between function calls. Similarly, the data for MATLAB® globals and persistents exists between function calls. Therefore, if your main function executes the generated code in multiple threads, indeterminate race conditions can occur.

To generate code more suitable for simultaneous execution in multiple threads, you can use the MultiInstanceCode configuration option. When you use this option, the generated code wraps data that can exceed the local thread storage into a structure definition, so that you can allocate thread-safe memory for that structure individually. Similarly, the code generator wraps persistent and global data into its own memory structure. When this memory should not be shared, you can allocate individual memory for the data structures used by a thread.

Topics

Reentrant Code Basics

Workflows