Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

macosx thread explicitly marked deleted

I'm building an application with C++11 threads, but I can't seem to get it to work with clang++ on MacOSX 10.9. Here is the simplest example I can find that causes the issues:

#include <thread>
#include <iostream>

class Functor {
  public:
    Functor() = default;
    Functor (const Functor& ) = delete;
    void execute () { 
      std::cerr << "running in thread\n";
    }
};

int main (int argc, char* argv[]) 
{
  Functor functor;
  std::thread thread (&Functor::execute, std::ref(functor));
  thread.join();
}

This compiles and runs fine on Arch Linux using g++ (version 4.9.2) with the following command-line:

$ g++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread

It also compiles and runs fine using clang++ (version 3.5.0, also on Arch Linux):

$ clang++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread

But fails on MacOSX 10.9.5, using XCode 6.1 (regardless of whether I include the -stdlib=libc++ option):

$ clang++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread
In file included from test_thread.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:332:5: error: attempt to use a deleted function
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:342:5: note: in instantiation of function template specialization
      'std::__1::__thread_execute<void (Functor::*)(), std::__1::reference_wrapper<Functor> , 1>' requested here
    __thread_execute(*__p, _Index());
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:354:42: note: in instantiation of function template specialization
      'std::__1::__thread_proxy<std::__1::tuple<void (Functor::*)(), std::__1::reference_wrapper<Functor> > >' requested here
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
                                         ^
test_thread.cpp:19:15: note: in instantiation of function template specialization 'std::__1::thread::thread<void (Functor::*)(), std::__1::reference_wrapper<Functor> , void>'
      requested here
  std::thread thread (&Functor::execute, std::ref(functor));
              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:1001:5: note: '~__nat' has been explicitly marked deleted
      here
    ~__nat() = delete;
    ^
1 error generated.

I can't figure out how to get around this, it seems like a compiler bug to me. For reference, the version of clang on that Mac is:

$ clang++ --version
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

Any ideas what it is I'm doing wrong? Thanks! Donald.

like image 438
jdtournier Avatar asked Nov 01 '22 13:11

jdtournier


1 Answers

The standard does not require the std::thread constructor - or the similar std::async for that matter - to unwrap a reference_wrapper when passed as the first argument with a pointer-to-member-function the way std::bind does. Pass a pointer to Functor instead of a reference_wrapper. (See Library Active Issues list DR2219.)

like image 145
Casey Avatar answered Nov 15 '22 07:11

Casey