totoro
totoro

Reputation: 161

How to call mexCallMATLAB from another thread

I'm creating a C++ module for MATLAB compiled by mex. I start a new thread in this module and call the matlab function myCallback from it:

mxArray *funcName = mxCreateString("myCallback");
mxArray *text - mxCreateString("AAA");
mxArray *call[2] = {funcName, text};
mexCallMATLAB(0, NULL, 2, call, "feval");
...

myCallback function:

function myCallback(text)
fprintf(1,'%s\n', text);
end

And it doesn't print anything after calling mexCallMATLAB. These functions work well in the same thread as matlab functions. What is a problem?

Upvotes: 1

Views: 830

Answers (1)

plasma
plasma

Reputation: 898

See here.

Basically, the answer is that the mex* functions are not thread safe. This includes mexCallMATLAB, mexPrintf, and friends (yes, even mexPrintf shouldn't be called from multiple threads). All mexCallMATLAB calls must be done from the main thread (i.e., the same thread of execution as MATLAB itself).

Upvotes: 2

Related Questions