为什么我不能编译这个简单的线程测试?

HWi*_*mer 2 c++ macos multithreading

I wanted to test some things with threads on my Macbook pro, but I can't get it to work.

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Run Code Online (Sandbox Code Playgroud)

this is the Version of clang installed on my machine. I tried to code some vector of threads but that didn't work so I went back and copied an example from SO.

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

using namespace std;

// The function we want to execute on the new thread.
void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello");

    // Do other things...

    // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
    t1.join();
}
Run Code Online (Sandbox Code Playgroud)

But I am getting a compiler error..

error: no matching constructor for initialization of
      'std::__1::thread'
    thread t1(task1, "Hello");
Run Code Online (Sandbox Code Playgroud)

I guess my machine is the problem, but why?

Sto*_*ica 5

Somehow, you built your code as C++03, probably by not providing a standard revision flag explicitly. libc++, the LLVM implementation of the standard library allows using <thread> in C++03 code. The source has conditional compilation of the following sort:

#ifndef _LIBCPP_CXX03_LANG
    template <class _Fp, class ..._Args,
              class = typename enable_if
              <
                   !is_same<typename __uncvref<_Fp>::type, thread>::value
              >::type
             >
        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
        explicit thread(_Fp&& __f, _Args&&... __args);
#else  // _LIBCPP_CXX03_LANG
    template <class _Fp>
    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
    explicit thread(_Fp __f);
#endif
Run Code Online (Sandbox Code Playgroud)

In C++11 and higher, the constructor adheres to the C++11 standard. Otherwise, it accepts only a callable without additional arguments. I managed to reproduce your error by providing the C++03 standard revision flag. The error even mentions this candidate:

#ifndef _LIBCPP_CXX03_LANG
    template <class _Fp, class ..._Args,
              class = typename enable_if
              <
                   !is_same<typename __uncvref<_Fp>::type, thread>::value
              >::type
             >
        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
        explicit thread(_Fp&& __f, _Args&&... __args);
#else  // _LIBCPP_CXX03_LANG
    template <class _Fp>
    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
    explicit thread(_Fp __f);
#endif
Run Code Online (Sandbox Code Playgroud)