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

 Akzeptierte Antwort

Kaustubha Govind
Kaustubha Govind am 5 Apr. 2011

1 Stimme

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 Kommentare

Sarfraaz
Sarfraaz am 6 Apr. 2011
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 ?
Kaustubha Govind
Kaustubha Govind am 6 Apr. 2011
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.
Sarfraaz
Sarfraaz am 6 Apr. 2011
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.
Kaustubha Govind
Kaustubha Govind am 6 Apr. 2011
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.
Sarfraaz
Sarfraaz am 7 Apr. 2011
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??
Kaustubha Govind
Kaustubha Govind am 7 Apr. 2011
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"?
Sarfraaz
Sarfraaz am 7 Apr. 2011
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.
Kaustubha Govind
Kaustubha Govind am 7 Apr. 2011
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).
Sarfraaz
Sarfraaz am 8 Apr. 2011
I need to process one frame at a time, but I want to stop the simulation once end of video file is reached.
Sarfraaz
Sarfraaz am 8 Apr. 2011
It would also be nice if you tell me how to handle simulink pause in Sfunction
Kaustubha Govind
Kaustubha Govind am 8 Apr. 2011
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!
Sarfraaz
Sarfraaz am 8 Apr. 2011
Thank you very much. How to handle pause? I dont see any callback function for pause.
Kaustubha Govind
Kaustubha Govind am 8 Apr. 2011
The above doc page has a link titled "Creating Pause Blocks": http://www.mathworks.com/help/toolbox/simulink/ug/f11-31373.html#bq0ht63
Sarfraaz
Sarfraaz am 11 Apr. 2011
Thank you very much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Simulink Coder finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by