【问题标题】:multithreading c++ passing arguments to a function多线程 C++ 将参数传递给函数
【发布时间】: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


【解决方案1】:

通过-std=c++11 就可以了;见下面我的成绩单。 (在 OS X 上运行。)

nathanst% g++ t.cpp 
t.cpp:13: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.
nathanst% g++ -std=c++11 t.cpp

【讨论】:

  • 谢谢!不过还有一个问题,什么是 -std=c++11 以及我为什么需要它?如果是因为我没有使用 c++11,那么我该如何更新到 c++11?
  • 您使用的编译器知道新的 C++11 功能,但默认情况下已将其关闭。 -std=c++11 告诉编译器打开该功能。在某些时候,较新的版本会默认打开它,您将不再需要该开关。
猜你喜欢
  • 1970-01-01
  • 2022-08-05
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多