无法将参数传递给std :: thread?

时间:2014-12-22 16:41:01

标签: multithreading macos c++11 clang stdthread

我试图使用std::thread。我的线程应该调用一个方法并传递一个struct作为参数,正如许多例子所示。除了我非常简单的代码不会编译。为了记录,我知道this问题,但似乎没有任何帮助我。

我在哪里打电话:

void Exporter::save() const {
    thread(write_to_disk, this->parameter).detach();
}

write_to_disk的签名:

void write_to_disk(const Parameter& parameter)

write_to_disk.cpp文件中的无名空间中定义。

我收到以下错误:

src/Exporter.cpp:65:5: error: no matching constructor for initialization of 'std::__1::thread'
    thread(write_to_disk, this->parameter).detach();
    ^      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:374: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:263: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:270:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    thread() _NOEXCEPT : __t_(0) {}
^

1 个答案:

答案 0 :(得分:5)

如果我这样做,对我来说没问题

clang++ -std=c++11 test.cpp
相关问题