MEX API Is Not Thread Safe
Do not call a single session of MATLAB® on separate threads from a MEX file. The MEX and Matrix Library APIs are not multi-threaded.
You can create threads from a C MEX file; however, accessing MATLAB from those threads is not supported. Do not call any MEX API functions
from the spawned threads, including printf, which is defined as
mexPrintf in the mex.h header file.
For more information, see Is it possible to start new threads from a C-MEX file? in MATLAB Answers™.