4.5

4.5 | 2 ratings Rate this file 158 downloads (last 30 days) File Size: 3.11 KB File ID: #21233

Mex Multithreading

by Dirk-Jan Kroon

 

26 Aug 2008 (Updated 02 Sep 2008)

Code covered by BSD License  

Example of (Windows) multi-threading in a MEX file

Download Now | Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.6 (R2008a)
Other requirements Windows OS
Zip File Content  
Other Files square.c,
test.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
26 Aug 2008 Nick Clark

Nice submission. Cute example and easy to understand. Check out Intel TBB for more scalability, especialy now it is available free of charge. Thanks again :D

29 Aug 2008 A B

Excellent introduction to multithreading inside a mex function.

16 Sep 2009 Jveer

can this capability be compiled into a standalone application using the matlab compiler?

Please login to add a comment or rating.
Updates
28 Aug 2008

Added Mutex example and replaced function _beginthread by _beginthreadex, which allows WaitForSingleObject in Matlab

02 Sep 2008

Now also works with the Matlab LCC compiler.

Tag Activity for this File
Tag Applied By Date/Time
multithreading Dirk-Jan Kroon 22 Oct 2008 10:16:15
multi threading Dirk-Jan Kroon 22 Oct 2008 10:16:15
threads Dirk-Jan Kroon 22 Oct 2008 10:16:15
mex Dirk-Jan Kroon 22 Oct 2008 10:16:15
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com