我正在尝试使用MacOS X Mavericks中的标准C ++(与XCode一起安装)中的线程运行一些代码。但是我遇到了一些错误。这是一个最小的工作示例:
#include <thread>
#include <iostream>
void run (int x) {
std::cout<<".";
}
int main (int argc, char const *argv[])
{
std::thread t(run);
}
我得到的错误:
minimal.cpp:10:17: error: no matching constructor for initialization of 'std::thread'
std::thread t(run,0);
^ ~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:372:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments
were provided
thread::thread(_Fp __f)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:261:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
thread(const thread&);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:268:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
thread() _NOEXCEPT : __t_(0) {}
^
1 error generated.
我已经能够跟踪问题到我的编译器定义_LIBCPP_HAS_NO_VARIADICS
,这是因为定义的
#if !(__has_feature(cxx_variadic_templates))
#define _LIBCPP_HAS_NO_VARIADICS
#endif
任何帮助,将不胜感激。
谢谢!
感谢pwny
和PeterT
,我发现了错误。
我只需要用clang++ -std=c++11 minimal.cpp
编译它就像一个魅力。我最后还需要一个t.join()
来防止执行错误。
我正在运行相同的应用程序的不同std :: thread行为。在xcode或仪器上(分析),在xcode上,单线程/多线程比率为0.6,在仪器中使用4线程阵列为3.7
这怎么可能?
Xcode run:
st...ok - lap: 4875 ms
st/8...ok - lap: 1205 ms
mt...ok - lap: 8330 ms
st/mt ratio:**0.6**
Instruments run:
st...ok - lap: 2182 ms
st/8...ok - lap: 545 ms
mt...ok - lap: 596 ms
st/mt ratio:**3.7**