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?
See here.
Basically, the answer is that the
mex*functions are not thread safe. This includesmexCallMATLAB,mexPrintf, and friends (yes, even mexPrintf shouldn’t be called from multiple threads). AllmexCallMATLABcalls must be done from the main thread (i.e., the same thread of execution as MATLAB itself).