【发布时间】:2016-10-05 19:09:10
【问题描述】:
我一直在尝试将参数传递给线程函数,但没有成功。我尝试阅读它(Passing multiple arguments to a threaded function),但我仍然无法弄清楚。这是我的代码:
#include <iostream>
#include <thread>
using namespace std;
void func(int t)
{
cout << t << endl;
}
int main()
{
thread t1(func,4);
t1.join();
return 0;
}
我在命令行中运行它(我使用 zsh 以防万一)这样做:
g++ test.cpp
./a.out
但我收到了这些错误:
thread_test.cpp:14:12: error: no matching constructor for initialization of 'std::__1::thread'
thread t1(func,4);
^ ~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:379: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/../include/c++/v1/thread:268: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/../include/c++/v1/thread:275:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
thread() _NOEXCEPT : __t_(0) {}
^
1 error generated.
如果这很重要,我使用的是 OSX 10.11.4 的 mac
另外,为了查看我正在编译的 c++ 版本,我运行了这个命令并将其输出:
g++ --version
配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer /SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1 苹果 LLVM 版本 7.3.0 (clang-703.0.31) 目标:x86_64-apple-darwin15.4.0 线程模型:posix 安装目录:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
【问题讨论】:
-
无法复制:ideone.com/BFRbzc。你用的是什么编译器?
-
Works with normal clang,所以 Apple 可能又搞砸了。不过很好的测试用例。
-
Possible duplicate. 确保使用
--std=c++11(或更高版本)进行编译。 -
我已经编辑了这个问题,所以您现在应该知道我正在使用的编译器,但我不知道如果我使用 --std=c++11 进行编译,我将如何发现跨度>
标签: c++ multithreading