Search code examples
multithreadingmacosc++11clangstdthread

Can't pass parameters to std::thread?


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) {}
^

Solution

  • Works fine for me if I do

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