0

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

4
  • 2
    Can't reproduce : ideone.com/BFRbzc . What compiler are you using? Commented Jun 6, 2016 at 8:41
  • 1
    Works with normal clang, so Apple probably broke something again. Good testcase though. Commented Jun 6, 2016 at 8:43
  • 2
    Possible duplicate. Make sure you're compiling with --std=c++11 (or higher). Commented Jun 6, 2016 at 12:01
  • I've edited the question so you should now know the compiler I am using but I don't know how I would find out if I were compiling with --std=c++11 Commented Jun 6, 2016 at 19:33

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Still one question though, what is -std=c++11 and why do I need it? If it's because I am not using c++11 then how can I update to c++11?
The compiler you are using knows about the new C++11 functionality, but has it turned off by default. -std=c++11 tells the compiler to turn on that functionality. At some point a newer version will have it turned on by default and you will no longer need that switch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.