任何代码都可能崩溃......而VC ++使用相同的代码。
这只是其中之一:
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
using namespace std;
int main()
{
mutex lock;
auto loop = [&lock](string name) {
for (int i = 0; i < 100; ++i)
{
lock.lock();
cout << name << ' ' << i << endl;
lock.unlock();
this_thread::sleep_for(10ms);
}
};
auto a = thread(loop, "A");
auto b = thread(loop, "B");
a.join();
b.join();
return 0;
}
使用tuple
构造函数访问某个空指针启动线程时,应用程序崩溃。
如果我将libc ++的源代码放入我的项目而不是链接到dylib,问题就不复存在了......
我之前很少在Mac OS X上编写C ++控制台应用程序。我想知道这是我的问题还是苹果的问题。
崩溃时的调用堆栈:
使用char const*&&
(不是char const *
)引用任何内容。
#0 0x0000000100000c7b in std::__1::__tuple_leaf<1ul, char const*, false>::__tuple_leaf<char const*, void>(char const*&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:222
#1 0x0000000100000c77 in std::__1::__tuple_impl<std::__1::__tuple_indices<0ul, 1ul>, main::$_0, char const*>::__tuple_impl<0ul, 1ul, main::$_0, char const*, main::$_0, char const*>(std::__1::__tuple_indices<0ul, 1ul>, std::__1::__tuple_types<main::$_0, char const*>, std::__1::__tuple_indices<>, std::__1::__tuple_types<>, main::$_0&&, char const*&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:416
#2 0x0000000100000c41 in std::__1::__tuple_impl<std::__1::__tuple_indices<0ul, 1ul>, main::$_0, char const*>::__tuple_impl<0ul, 1ul, main::$_0, char const*, main::$_0, char const*>(std::__1::__tuple_indices<0ul, 1ul>, std::__1::__tuple_types<main::$_0, char const*>, std::__1::__tuple_indices<>, std::__1::__tuple_types<>, main::$_0&&, char const*&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:416
#3 0x0000000100000c2a in std::__1::tuple<main::$_0, char const*>::tuple<main::$_0, char const*, false>(main::$_0&&, char const*&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:569
#4 0x0000000100000bf5 in std::__1::tuple<main::$_0, char const*>::tuple<main::$_0, char const*, false>(main::$_0&&, char const*&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:569
#5 0x0000000100000bd2 in std::__1::thread::thread<main::$_0&, char const (&) [2], void>(main::$_0&&&, char const (&&&) [2]) at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:352
#6 0x0000000100000ab5 in std::__1::thread::thread<main::$_0&, char const (&) [2], void>(main::$_0&&&, char const (&&&) [2]) at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:359
#7 0x0000000100000998 in main at /Users/blue/Documents/_/Test1/Test1/main.cpp:27
#8 0x00007fff863545c9 in start ()
有趣的是代码在发布模式下工作。虽然其他一些代码在调试模式下工作但不是发布模式。