gpt4 book ai didi

multithreading - macosx 线程显式标记为已删除

转载 作者:行者123 更新时间:2023-12-04 16:35:52 25 4
gpt4 key购买 nike

我正在使用 C++11 线程构建一个应用程序,但我似乎无法让它在 MacOSX 10.9 上与 clang++ 一起工作。这是我能找到的导致问题的最简单示例:

#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();
}

这在 Arch Linux 上使用 g++(版本 4.9.2)和以下命令行编译并运行良好:
$ g++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread

它还可以使用 clang++(版本 3.5.0,也在 Arch Linux 上)编译并运行良好:
$ clang++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread

但在 MacOSX 10.9.5 上失败,使用 XCode 6.1(无论我是否包含 -stdlib=libc++ 选项):
$ 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.

我不知道如何解决这个问题,对我来说这似乎是一个编译器错误。作为引用,该 Mac 上的 clang 版本是:
$ 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

任何想法我做错了什么?
谢谢!
唐纳德。

最佳答案

该标准不要求 std::thread构造函数 - 或类似 std::async就此而言 - 打开 reference_wrapper当使用指向成员函数的指针作为第一个参数传递时 std::bind做。将指针传递给 Functor而不是 reference_wrapper . (见 Library Active Issues list DR2219。)

关于multithreading - macosx 线程显式标记为已删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27109545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com