Thread in C++ in MacOS X

11,631

Thanks to pwny and PeterT, I figured out the error.

I just needed to compile with clang++ -std=c++11 minimal.cpp and it worked like a charm. I also needed a t.join() at the end to prevent an execution error to happen.

Share:
11,631

Related videos on Youtube

spalac24
Author by

spalac24

Updated on June 04, 2022

Comments

  • spalac24
    spalac24 almost 2 years

    I'm trying to run some code using threads in standard C++ (installed with XCode) in MacOS X Mavericks. But I'm getting some errors. Here's a minimal working example:

    #include <thread>
    #include <iostream>
    
    void run (int x) {
        std::cout<<".";
    }
    
    int main (int argc, char const *argv[])
    {
        std::thread t(run);
    }
    

    The error I'm getting:

    minimal.cpp:10:17: error: no matching constructor for initialization of 'std::thread'
    std::thread t(run,0);
                ^ ~~~~~
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:372: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/../lib/c++/v1/thread:261: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/../lib/c++/v1/thread:268:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    thread() _NOEXCEPT : __t_(0) {}
    ^
    1 error generated.
    

    I've been able to track the problem to my compiler defining _LIBCPP_HAS_NO_VARIADICS, which is defined because of

    #if !(__has_feature(cxx_variadic_templates))
    #define _LIBCPP_HAS_NO_VARIADICS
    #endif
    

    Any help would be appreciated.

    Thank you!

    • anthonyvd
      anthonyvd about 10 years
      What command line arguments are you passing to clang?
    • spalac24
      spalac24 about 10 years
      I'm compiling with g++, I only do g++ minimal.cpp. clang++ minimal.cpp also fails.
    • anthonyvd
      anthonyvd about 10 years
      add --std=c++11 and see if it helps
    • spalac24
      spalac24 about 10 years
      It does help in the compiling, but running gives me libc++abi.dylib: terminating .Abort trap: 6
    • anthonyvd
      anthonyvd about 10 years
      that's probably a different issue. Try running it through gdb and see where it breaks.
    • PeterT
      PeterT about 10 years
      @user3267581 try adding t.join(); at the end of main
    • spalac24
      spalac24 about 10 years
      Yep, that did it. I just needed to add a t.join(). Thank you very much. I'd like to give you an accepted answer, but as this is a comment... Let me know if I should answer myself. (So if this happens to anyone else they will know)
    • PeterT
      PeterT about 10 years
      @user3267581 just answer yourself
  • vforvendetta
    vforvendetta almost 9 years
    Thanks for the clang tip. The default g++ doesn't work.
  • Fisher Coder
    Fisher Coder about 5 years
    How can we use g++ in this case? I know clang++ -std=c++11 works for me as well.