Multithreading with Matlab
You can only call the mx* and mex* functions from the MATLAB main thread. You can write multithreaded MEX files providing these do their work at a level below the mx interface. If you want multiple MATLAB interpreters, you need multiple MATLAB processes. One way is through the Parallel Computing Toolbox as pointed out by @You. This gives you PARFOR
loops and SPMD
blocks for running things simultaneously.
You'd probably be better off using MATLABs built-in multithreading features such as parfor
. In fact, many MATLAB functions are already multithreaded (including matrix operations), so there should be no need for you to parallelize things yourself apart from replacing for
with parfor
. (In general, while
loops cannot be paralellized.)
Your best option is parfor
. if you're a student you can get parallel toolbox for pretty cheap. even full price is not much if you're serious about performance. Your code above will be error prone and hard to test. using parfor
is intuitive and clean.