Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

c++ - std::call_once throws std::system_error (Unknown error -1)

I was using the C++ OpenCL wrapper and I was wondering why my program crashed. I discovered any call to std::call_once was throwing an error.

#include <mutex>

int main() {
    static std::once_flag f;
    std::call_once(f, []{});
}

Program output:

terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1

Here's the output of 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As said in the comments by Praetorian, std::call_once need to call the system threading library. More specifically, it will call __gthread_once. If the executable is not linked to pthread, that function will return -1 and will then cause the exception to be thrown.

To make a program pthread-enabled -pthread option needs to be passed both to compiler and linker, as stated in gcc documentation. Just linking with -lpthread sometimes is not enough and not just because of additional macros. For CMake users there is an out of the box module that can help adding support for pthread (or any system threading library) that can be used like that:

find_package(Threads REQUIRED)
target_link_libraries(myTarget PRIVATE Threads::Threads)

This will add any required compile and link flags if necessary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...