Can't pass parameters to std::thread?

≯℡__Kan透↙ 提交于 2019-12-23 20:41:33

问题


I'm trying to use std::thread. My thread is supposed to call a method and pass a struct as a parameter, as so many examples show. Except my very simple code won't compile. For the record, I'm aware of this question but nothing there seems to help me.

Where I call the thread:

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

The signature of write_to_disk:

void write_to_disk(const Parameter& parameter)

write_to_disk is defined in a nameless namespace in the .cpp file.

I get the following error:

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:


Works fine for me if I do

clang++ -std=c++11 test.cpp


来源:https://stackoverflow.com/questions/27606782/cant-pass-parameters-to-stdthread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!