不确定的std::system_error:what():不允许操作

Undeterministic std::system_error: what(): Operation not permitted

本文关键字:不允许 操作 what std system 不确定 error      更新时间:2023-10-16

我正在尝试运行我的程序,每隔几次运行就会出现一个错误:

terminate called after throwing an instance of 'std::system_error'
what():  Operation not permitted

我的代码在这里可用:https://github.com/Qabrt/turnstiles

gdb输出:

Thread 31 "trivial_test" received signal SIGABRT, Aborted.
[Switching to thread 31 (Thread 0x7fff8a7fc700 (LWP 8716))]
#0  0x00007ffff6e7f83b in raise () from /lib64/libc.so.6
(gdb) bt
#0  0x00007ffff6e7f83b in raise () from /lib64/libc.so.6
#1  0x00007ffff6e81081 in abort () from /lib64/libc.so.6
#2  0x00007ffff78670e5 in __gnu_cxx::__verbose_terminate_handler ()
at /var/tmp/portage/sys-devel/gcc-7.3.0-r3/work/gcc-7.3.0/libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff7864cb6 in __cxxabiv1::__terminate (handler=<optimized out>)
at /var/tmp/portage/sys-devel/gcc-7.3.0-r3/work/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:47
#4  0x00007ffff7864d01 in std::terminate ()
at /var/tmp/portage/sys-devel/gcc-7.3.0-r3/work/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:57
#5  0x00007ffff789243f in std::execute_native_thread_routine (__p=0x555555790f80)
at /var/tmp/portage/sys-devel/gcc-7.3.0-r3/work/gcc-7.3.0/libstdc++-v3/src/c++11/thread.cc:91
#6  0x00007ffff7bbd96a in start_thread () from /lib64/libpthread.so.0
#7  0x00007ffff6f4d11f in clone () from /lib64/libc.so.6

g++——版本

g++ (Gentoo 7.3.0-r3 p1.4) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我正在使用-lpthread标志进行编译:

/usr/bin/c++    -Wall -Wunused-function -Wwrite-strings -Wformat -Wformat-security -Wparentheses -Wsequence-point -Wno-system-headers -Werror -Winit-self  -g -O0 -fstack-protector-all -D_GLIBXX_DEBUG -D_GLIBXX_DEBUG_PEDANTIC  -rdynamic CMakeFiles/trivial_test.dir/trivial_test.cpp.o  -o trivial_test ../libturnstile_lib.a -lpthread

如何获取有关该问题的更多信息?

在将函数设置为noexcept后,我能够找到堆栈跟踪,显示我正在对不存在的互斥体的unique_lock调用condition_variable::wait()。只是我调用wait()的Semaphore对象为null。因此,unique_lock抛出的system_error如下https://en.cppreference.com/w/cpp/thread/unique_lock/lock

首先,您需要查明错误是来自std::thread运行的函数还是来自std::thread本身。如果函数抛出异常,它将被std::thread启动器函数捕获,并调用terminate。如果您将函数设为noexcept,那么terminate将在被捕获之前被调用,并且您将在堆栈跟踪中看到它是从哪里抛出的(GCC的后续版本不会捕获异常,所以这是自动发生的)。

如果异常来自std::thread本身,则意味着您的程序已链接到libc.so.6pthread_create的伪定义,而不是libpthread.solibpthread.a中的真实定义

使用ldd查看您的程序是否链接到共享的libpthread.so。如果是,则表示您的工具链有问题(应该使用libpthread.so中的定义,而不是libc.so中的弱符号)。如果你是静态链接,你可能需要确保libpthread.a中的所有符号都包含在你的程序中,例如使用:

-Wl,--whole-archive -pthread -Wl,--no-whole-archive

注意:您应该使用-pthread而不是-lpthread,因为这允许GCC确保它被放置在链接命令中的正确位置。