Max_in_place is a sample MEX file that shows a generic framework for multi-threaded processing in MEX functions.
Syntax: max_in_place(matrix1, matrix2)
The 2 input matrices must be real non-sparse and have the same number of elements. The matrix1 input is updated in-place (without memory reallocation) with the maximal value of corresponding elements in the two matrices.
Multi-threading is controlled via Matlab's builtin maxNumCompThreads function.
The code can easily be adapted for other processing types.
A detailed description is found at: http://UndocumentedMatlab.com/blog/multi-threaded-mex
Yair Altman (2021). max_in_place - example of multi-threaded MEX function (https://www.mathworks.com/matlabcentral/fileexchange/68237-max_in_place-example-of-multi-threaded-mex-function), MATLAB Central File Exchange. Retrieved .
Maybe line 74 should be deleted, because it is somewhat misleading.
/*mexPrintf("Starting thread #%d: idx=%d:%d\n", ThreadID, startIdx, endIdx); */
If you uncommen this and run, it will crash MATLAB. I tried this in R2020a on Windows and R2018a on Linux, both crashed.
"Since the MEX API is not thread safe ... including printf...", from https://www.mathworks.com/matlabcentral/answers/101658-is-it-possible-to-start-new-threads-from-a-c-mex-file
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!