How do I write my multithreading code correctly so that it is faster than a single thread?
How did you time your program?
I ran a modified version of your program (I didn't have &quot;mex.h&quot; so I rewrote the parts that needed it).  When I ran your program with 1, 2, and 4 threads, I got the following results:
1 thread  = 9.38 sec
2 threads = 4.93 sec
4 threads = 2.81 sec

So it seems to be working for me.  How did you time your program?  You may have been using a function such as clock() that measures cpu time across all cores, instead of measuring wall clock time.  See this Stackoverflow question for more information on that.