Mathew
Mathew

Reputation: 1430

multithreading c++ passing arguments to a function

I have been trying to pass arguments to a thread function but haven't been able to. I tried reading about it (Passing multiple arguments to a threaded function) but I still couldn't figure it out. Here is my code:

#include <iostream>
#include <thread>

using namespace std;

void func(int t)
{
    cout << t << endl;
}

int main()
{
    thread t1(func,4);
    t1.join();
    return 0;
}

I ran it in the command line (I use zsh incase that matters) by doing this:

g++ test.cpp
./a.out

but I got these errors:

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.

in case this is important I am using a mac with OSX 10.11.4

Also, to see what versions of c++ I was compiling with I ran this command and got this out put:

g++ --version

Configured with: --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 Apple LLVM version 7.3.0 (clang-703.0.31) Target: x86_64-apple-darwin15.4.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Upvotes: 0

Views: 830

Answers (1)

Nathan S.
Nathan S.

Reputation: 5388

Pass -std=c++11 and it will work; see my transcript below. (Running on 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

Upvotes: 1

Related Questions