The problem was because of compiler's optimization. The original codes shown above include some dummy for-loops for time-delay. That was simply taken out of the code by compiler for optimization, which resulted in a super quick execution of the new thread.
The new simplified codes shown below include some meaningful addition and subtraction within the for-loops.
And the result shows expected asynchronous execution of each thread.
// This code uses "printf", which I didn't confirm thread-safty in Matlab.
// This code does not utilize mutex or other concurrent programming tools.
// The only purpose of this code is to show that C++11 can be used in
// Matlab 2013a with Microsoft Visual Studio 2012 Express.
#include "mex.h"
int count_thread1 = 0;
int count_thread2 = 0;
void hello()
{
count_thread2 = 0;
for(int i=0; i<=10000; i++){
for (int j=1;j<=20000;j++){
count_thread2 = i-j-1;
}
count_thread2++;
printf("2: %d , %d\n", count_thread1, count_thread2); // Not sure if printf is thread-safe in Matlab. But it works in this particular example
}
}
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
count_thread1 = 0;
std::thread t(hello);
for (int i=1;i<=10000;i++)
{
for (int j=1;j<=20000;j++){
count_thread1 = -i+j-1;
}
count_thread1++;
mexPrintf("1: %d , %d\n", count_thread1, count_thread2);
}
mexPrintf("\n");
t.join();
mexPrintf("Done\n");
}
The output looks like:
2: 10744 , -10033
1: 10743 , -10033
2: 10743 , -10032
2: 10742 , -10031
2: 10742 , -10030
1: 10742 , -10032
2: 10742 , -10029
2: 10741 , -10028
2: 10741 , -10027
2: 10741 , -10026
1: 10741 , -10029
2: 10741 , -10025
1: 10740 , -10025
.......................
2: 10718 , -10002
1: 10717 , -10002
2: 10717 , -10001
1: 10716 , -10001
2: 10716 , -10000
1: 10715 , -10000
1: 10714 , -10000
1: 10713 , -10000