Multithreading in S function

Hello All,
I am trying to create a S function in matlab which will read data from a video file and send the frame data via the output port.
The each and every frame information is read using a thread (External framework thread invoked through c++)and the information is sent to in the output pin using mdlOutputs.
The S function I created starts the read thread in mdlStart, but the mdlOutputs is called only after the read thread is stopped. hence mdlOutputs give me only the last frame information for every simulation time step.
I would like to know if it is possible to explicitly invoke mdlOutputs from read thread to receive the each and every frame information.
Thank you very much.
Please suggest if Multithreading is possible with S functions.
Regards Sarfraaz

 Accepted Answer

Kaustubha Govind
Kaustubha Govind on 5 Apr 2011
The rules for multithreading in S-functions are pretty much the same as those in MEX-functions.
mdlOutputs must be invoked by the Simulink engine, because it needs the current SimStruct passed in. It is never advisable to call S-function call-back methods from another callback method.
mdlStart is called only once during model execution (at the beginning), whereas mdlOutputs is called at every time step - it seems like you should really be doing your reading in mdlOutputs directly, and not in mdlStart. Is there a reason you chose your current workflow?

14 Comments

Hello Mr. Govind,
The reading of the frames is done using an external framework invoked from the sfunction. It runs as a separate thread and i need to send the frame information through the output port. Is there a any work around??
i tried copying the simstruct in a global variable and tried to invoke mdlOutputs but it doesn't work. is there any event that could be fired to invoke mdlOutputs explicitly ?
I'm sorry, but I still don't understand why you don't call this external framework from mdlOutputs directly. That seems like the right way to go in your case.
Even if i call the external framework from mdlOutputs the external framework will run has a separate thread and extract the frame information. I need to find a way to update the output port from the other thread. so that i get information about all the frames.
Is there no way to aggregate the data read by the threads and pass them all together back to the S-function? Note that it is not safe to call any MATLAB API functions (mdlOutputs and any SimStruct-related functions included) from a thread - you must also call them from the main process that spawns the threads.
Thanks for the suggestion. I have synchronized the operation between my thread and MATLAB thread.
I am still stuck in the step where is should terminate the MATLAB thread when my other thread ends before the end of simulation time. IS it possible to call mdlTerminate once my other thread is terminated??
You must leave all invocations to S-function callback methods (like mdlTerminate) to the Simulink engine. Could you please elaborate on what you do in this "MATLAB thread"?
Most of the computation is done in my thread. MaATLAB thread only initializes the arrays, starts the other thread and updates the output port.
I want to stop the MATLAB thread once my other thread is terminated. If not the data from the last frame read by my thread is given out as output for every simulation step.
So would you like to read all the frames only once and send out multiple frames at every time step? It seems like the right thing to do would be to process one frame at a time (so the last frame read by your thread is the latest one to be processed).
I need to process one frame at a time, but I want to stop the simulation once end of video file is reached.
It would also be nice if you tell me how to handle simulink pause in Sfunction
I don't know of a way to stop the simulation from the S-function directly, but you can use the Stop Simulation block (http://www.mathworks.com/help/toolbox/simulink/slref/stopsimulation.html) in your model to do this. You can output a non-zero valued signal from the S-function when you want to stop the simulation and connect that signal to the Stop Simulation block. HTH!
Thank you very much. How to handle pause? I dont see any callback function for pause.
The above doc page has a link titled "Creating Pause Blocks": http://www.mathworks.com/help/toolbox/simulink/ug/f11-31373.html#bq0ht63
Thank you very much.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!