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

Reentrant Code

Reentrant code is a reusable programming routine that multiple programs can use simultaneously.

API for Generated Reusable Code

Use generated reusable code.

Specify Generation of Reentrant Code

Generate reentrant code in the MATLAB Coder™ app or at the command line.

Call Reentrant Code in a Single-Threaded Environment

Use generated reentrant code in a single-threaded environment.

Call Reentrant Code in a Multithreaded Environment

Use generated reentrant code in a multithreaded environment.

Workflows

Generate Reentrant C Code from MATLAB Code

Generate reusable, reentrant C code.

Call Reentrant Code with No Persistent or Global Data (UNIX Only)

Generate reentrant code that uses POSIX thread (pthread) libraries.

Call Reentrant Code — Multithreaded with Persistent Data (Windows Only)

Generate reentrant code that uses libraries that are specific to Windows® platforms.

Call Reentrant Code — Multithreaded with Persistent Data (UNIX Only)

Generate reentrant code that uses POSIX thread (pthread) libraries.