Mex Multithreading
Nota del editor: This file was selected as MATLAB Central Pick of the Week
Nowadays, Matlab is multi-threaded but most MEX files are still single threaded. This is an simple example of c-code for MEX file multi-threading.
The mex function "y = square(x)" is written in c-code and starts 2 worker threads. The first worker thread will square all values on the even input indexes x[0] x[2] .. x[n], and the second worker thread all odd indexes x[1] x[3] .. x[n]. Also demonstrates thread safety by using Mutex to lock percentage variable.
example,
mex square.c -v;
y = square([1 2 3 4 5 6 7 8 9])
note:
The function uses <windows.h> (function sleep ) and <process.h> ( function _beginthreadex ) which are included with visual studio and some other compilers.
This example is tested with Windows Vista 64 and Visual Studio 2008 compiler, and with (default) LCC compiler and Vista 32.
Citar como
Dirk-Jan Kroon (2024). Mex Multithreading (https://www.mathworks.com/matlabcentral/fileexchange/21233-mex-multithreading), MATLAB Central File Exchange. Recuperado .
Compatibilidad con la versión de MATLAB
Compatibilidad con las plataformas
Windows macOS LinuxCategorías
Etiquetas
Agradecimientos
Inspiración para: max_in_place - example of multi-threaded MEX function
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Descubra Live Editor
Cree scripts con código, salida y texto formateado en un documento ejecutable.
Versión | Publicado | Notas de la versión | |
---|---|---|---|
1.0.0.0 | Now also works with the Matlab LCC compiler. |