How to share a C struct among C S-functions?

放肆的年华 提交于 2019-12-06 02:37:11
remus

I have successfully implemented and tested the DLL idea (i.e. gather all model parameters in a struct inside a DLL - plus any necessary calculations - and link it to other C S-functions). Simulink is a single process and I believe the simulation is a single thread (if not there are ways to make it thread-safe).

The structure with model parameters from the DLL acts as a singleton (instantiated only once per simulation and shared among other S-functions).

Update (some replies from mathworks):

The simulation part from Simulink is always single threaded. Hence, there are no thread-safety concerns for the shared DLL.

The global variables for the different S-function blocks would share the same memory location. This implies that a single instance of the shared library will be loaded for all of Simulink. Therefore all S-function instances that load the shared library will be referring to the same shared library and data.

Alternatively you can also consider using the Data Store Blocks to implement global variables. This will require a signal connection from each S-function to the appropriate Data Store Blocks. But a connection between S-functions would not be needed.

You could allocate and store the values in a separate DLL, although it seems complicated. Sharing DLLs between s-functions a few years ago, I found Matlab/Simulink's DLL loading to be opaque and hard to manage (e.g. loading dependent DLLs).

A simpler approach - have one s-function allocate the structs in its DWork vector :

http://www.mathworks.com/help/simulink/sfg/about-dwork-vectors.html

then store the pointer values to some workspace / global variables, where they can be accessed later on by the other s-functions.

Probably the easiest way to write to workspace / global variables using mexEvalString, and to read from them using mexGetVariable (Mathworks documentation is great for these and other functions)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!