1

I am trying to start a thread t:

#include <iostream>
#include <fstream>
#include <string>
#include <thread>

void function(int p1, int p2, int p3){
  std::cout<<p1<<p2<<p3<<std::endl;
}

int main(int argc, char const *argv[]) {
  std::cout<<"starting"<<std::endl;
  std::thread t(function, 1, 2, 3);
  std::cout<<"created thread"<<std::endl;
  t.join();
  std::cout<<"end"<<std::endl;
  return 0;
}

My compiler tells me this:

doesntwork.cpp:12:15: error: no matching constructor for
      initialization of 'std::thread'
  std::thread t(function, 1, 2, 3);
              ^ ~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:408:9: note: 
      candidate constructor template not viable: requires single
      argument '__f', but 4 arguments were provided
thread::thread(_Fp __f)
        ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:289:5: note: 
      candidate constructor not viable: requires 1 argument, but 4
      were provided
    thread(const thread&);
    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:296:5: note: 
      candidate constructor not viable: requires 0 arguments, but
      4 were provided
    thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
    ^
1 error generated.

In the first case it tells me that for the thread t, there is no constructor that can use more than 1 parameter, while if I just remove the arguments (p1, p2, p3) it doesn't work either because I am not passing any argmuent....

Compiler information:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr 

--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

built command used: g++ doesntwork.cpp -o doesntwork.out

Is there something different you have to do when compiling with threads? Am I missing something very obvious?

14
  • 1
    #import - that is not c++, please edit your question to contain minimal reproducible example Commented Oct 20, 2018 at 2:46
  • yeah sorry meant #include Commented Oct 20, 2018 at 2:48
  • 1
    use -std=c++11 compile switch Commented Oct 20, 2018 at 3:22
  • 1
    adding -std=c++11 worked! Thanks @M.M I had no idea that was a thing that could go wrong... Commented Oct 20, 2018 at 3:24
  • 1
    ok, I voted to reopen, once it is open someone will post that as an answer Commented Oct 20, 2018 at 3:27

1 Answer 1

2

On macOS, g++ (from Xcode: Version 10.0 (10A255)) is aliased to clang which by default does not work with c++11 threads. To solve the problem you have to use the -std=c++11 switch.

Example:

g++ -std=c++11 fileToCompile.cpp -o outputFile.out

This should let you compile c++ code using c++11 threads.

Thank you to @M.M for providing the answer above in the comments.

Sign up to request clarification or add additional context in comments.

Comments

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.