c++ 11 > <线程在Matlab不能正常工作吗?它的工作原理。

4视图(30天)
我用的是32位版本的Matlab在Windows 7 2013 64位操作系统。
我安装了微软Visual Studio 2012支持许多c++ 11特性的表达。万博1manbetx然后我做了“墨西哥人设置”。的默认目录信息安装Visual c++不同(C: \ Program Files \…)从编译器默认的安装位置,所以我纠正它(C:\Program Files (x86)\……)。
然后我写了一个非常简单的代码,称为mexfunction2.cpp。在这段代码中,生成一个新线程更新一个全局变量,和主要原始线程打印它。代码是这样的:
#包括“mex.h”
#包括< <线程> >
intcount_thread1 = 0;
intcount_thread2 = 0;
无效你好()
{
(很久int i = 1; i < 1000000000;我+ +)
{
count_thread2 + +;/ /更新全球变量
}
}
/ / Matlab mexfunction
无效mexFunction (int nlhs mxArray * plhs [], int nrhs, const mxArray * prhs [])
{
count_thread1 = 0;
count_thread2 = 0;
std::线程t(你好);
/ / t.join ();/ /新线程之前完成印刷
(int j = 1; < 100; j + +)
{
count_thread1 = 1;
(很久int k = 1; k < 10000000; k + +);/ /一些延迟printf之间
mexPrintf (“% d % d:“、count_thread1 count_thread2);/ /打印变量
/ /如果(j = = 3)
/ / t.join ();/ /查看如果错了手动调试
}
mexPrintf (“\ n”);
t.join ();
}
然后输出是这样的:
1 0:1 0:1 0:1 0:1 0:1 0:1 0:1 0:1 0:1 0:1 0:
不应该这样的。我希望上面的零有越来越多。我改变的位置t.join()代码的不同部分,很明显,没有新线程运行背景和线程模拟。检查,如果我做了一些严重的错误,我写了一个等价的代码在Visual Studio,这是完全相同的编译器(我猜,因为这是唯一Visual c++编译器机):
#包括“stdafx.h”
#包括< <线程> >
#包括< < iostream > >
intcount_thread1 = 0;
intcount_thread2 = 0;
无效你好()
{
(很久int i = 1; i < 1000000000;我+ +)
{
count_thread2 + +;/ /更新全球变量
}
}
/ / Microsoft Visualc++控制台应用程序的主要功能
int_tmain (int命令行参数个数,_TCHAR * argv [])
{
count_thread1 = 0;
count_thread2 = 0;
std::线程t(你好);
/ / t.join ();
(int j = 1; < 100; j + +)
{
count_thread1 = 1;
(很久int k = 1; k < 10000000; k + +);/ /一些延迟
std:: cout < <”“< < count_thread1 < <”“< < count_thread2 < <”:“;/ /打印出
/ /如果(j = = 3)
/ / t.join ();
}
t.join ();
返回0;
}
然后这个应用程序显示的输出:
1 12653651:26374767:39375259:52980165:65144711:79238930:994:93211 1 107331017:120377626:133448865:147428833:161250113:17522040:……
这正是我期望从一个多线程程序。
所以,我的问题很简单。为什么使用相同的编译器的代码显示不同的结果呢?为什么墨西哥人文件不产生一个新线程呢?
谢谢你!

接受的答案

金-秀唱
金-秀唱 2013年4月12日
问题是由于编译器的优化。上面所示的原始代码包括一些假时滞的for循环。只是的代码编译器的优化,导致一个超级快速新线程的执行。
新的简化代码所示包括一些有意义的加法和减法在for循环。
和预期结果显示每个线程的异步执行。
/ /这段代码使用printf“,我没有确认thread-safty Matlab。
/ /这段代码不使用互斥对象或其他并发编程工具。
/ /这段代码的唯一目的是表明c++ 11中可以使用
2013 / / Matlab与微软Visual Studio 2012中表达。
# include“mex.h”
# include <线程 >
# include < stdio . h >
int count_thread1 = 0;
int count_thread2 = 0;
空白hello ()
{
count_thread2 = 0;
(int i = 0;< = 10000;我+ +){
(int j = 1; < = 20000; j + +) {
count_thread2 = i-j-1;
}
count_thread2 + +;
printf (“2:% d % d \ n”、count_thread1 count_thread2);/ /不确定如果在Matlab printf是线程安全的。但在这个特殊的例子
}
}
空白mexFunction (int nlhs mxArray * plhs [], int nrhs const mxArray * prhs [])
{
count_thread1 = 0;
std::线程t(你好);
(int i = 1;我+ +)< = 10000;
{
(int j = 1; < = 20000; j + +) {
count_thread1 =我+ j - 1;
}
count_thread1 + +;
mexPrintf (“1:% d % d \ n”、count_thread1 count_thread2);
}
mexPrintf (“\ n”);
t.join ();
mexPrintf (“\ n”);
}
输出:
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

更多的答案(0)

类别

找到更多的在从MATLAB编写C函数调用(墨西哥人文件)帮助中心文件交换

社区寻宝

找到宝藏在MATLAB中央,发现社区如何帮助你!

开始狩猎!