【发布时间】:2018-07-29 22:09:22
【问题描述】:
我正在使用 C++ OpenCL 包装器,我想知道为什么我的程序崩溃了。我发现任何对 std::call_once 的调用都会引发错误。
#include <mutex>
int main() {
static std::once_flag f;
std::call_once(f, []{});
}
程序输出:
terminate called after throwing an instance of 'std::system_error' what(): Unknown error -1
这是g++ -v的输出:
Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp Thread model: posix gcc version 8.1.1 20180531 (GCC)
【问题讨论】:
-
您的
CXX_FLAGS中有-pthread吗? -
没有。我想它需要它工作吗?
-
是的,由于底层实现使用 pthreads,您需要将该标志传递给 g++。除其他外,它会将您的程序链接到
libpthread。 -
@Praetorian 哇,它确实有效!谢谢你。如果您想将其发布为答案,我很乐意接受。
-
我没有发布答案,因为我不想挖掘文档说明为什么
-pthread有帮助:) 如果您愿意,您可以自己发布答案。