c++ - libc++abi.dylib:以 std::__1::system_error 类型的未捕获异常终止:互斥锁失败:参数无效

标签 c++ cmake c++17

我有一些线程池代码已经在我的 Ubuntu 机器上运行了很长一段时间。该项目是一个 CMake 项目,我相信它使用的是最新版本的 g++ 作为编译器。

我刚刚尝试在 Mac 上运行代码,CMake 尝试使用 AppleClang 12.0.0.12000032,我遇到了

libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument

AppleClang 是否缺少 std::shared_mutexstd::mutex?这些是 c++17 的特性。我需要做些什么才能在这台 Mac 上启用 c++17 吗?我确实提到它在我的 CMakeLists.txt 文件中是必需的。

这是完整的构建输出:

(base) clare.obrien.brown@Clares-Air build % cmake ..
-- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/clare.obrien.brown/ssme/build
(base) clare.obrien.brown@Clares-Air build % make
Scanning dependencies of target ssme_test
[ 16%] Building CXX object test/CMakeFiles/ssme_test.dir/test-main.cpp.o
[ 33%] Building CXX object test/CMakeFiles/ssme_test.dir/test_ada_pmmh_mvn.cpp.o
[ 50%] Building CXX object test/CMakeFiles/ssme_test.dir/test_parameters.cpp.o
In file included from /Users/clare.obrien.brown/ssme/test/test_parameters.cpp:5:
/Users/clare.obrien.brown/ssme/include/ssme/parameters.h:333:58: warning: unused parameter 'trans_p' [-Wunused-parameter]
float_t null_trans<float_t>::log_jacobian(const float_t& trans_p)
                                                         ^
1 warning generated.
[ 66%] Building CXX object test/CMakeFiles/ssme_test.dir/test_thread_pool.cpp.o
/Users/clare.obrien.brown/ssme/test/test_thread_pool.cpp:13:46: warning: unused parameter 'obs_data' [-Wunused-parameter]
    static double d(param_t nums, obs_data_t obs_data) {
                                             ^
/Users/clare.obrien.brown/ssme/test/test_thread_pool.cpp:54:45: warning: unused parameter 'od' [-Wunused-parameter]
                [](param_t nums, obs_data_t od) -> double{
                                            ^
/Users/clare.obrien.brown/ssme/test/test_thread_pool.cpp:82:40: warning: unused parameter 'ydata' [-Wunused-parameter]
    double d(param_t theta, obs_data_t ydata) {
                                       ^
3 warnings generated.
[ 83%] Building CXX object test/CMakeFiles/ssme_test.dir/test_utils.cpp.o
[100%] Linking CXX executable ssme_test
[100%] Built target ssme_test
(base) clare.obrien.brown@Clares-Air build % cd ../test 
(base) clare.obrien.brown@Clares-Air test % ../build/test/ssme_test 
libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
zsh: abort      ../build/test/ssme_test

最佳答案

这是抛出异常的地方:https://github.com/llvm/llvm-project/blob/main/libcxx/src/mutex.cpp#L35

这通常发生在 .lock() 在尚未构造或已被破坏的 mutex 上调用时。当在 main() 之前或在 main() 返回之后访问具有静态存储持续时间的互斥量时,可能会发生这种情况。您需要检查启动或关闭逻辑,以确保 mutex 的生命周期超过尝试使用它的客户端的生命周期。

/Applications/Utilities/Console.app 可以在抛出未捕获的异常时为您提供应用程序的堆栈跟踪。这可能有助于您的调试工作。使用 -g 编译可能会为堆栈跟踪提供更好的调试信息。

关于c++ - libc++abi.dylib:以 std::__1::system_error 类型的未捕获异常终止:互斥锁失败:参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66773247/

相关文章:

c++ - 嵌套静态链接库和一个可怕的错误

C++11 兼容的线性分配器实现

c++ - 将 libsodium.a 链接到共享对象时出错

c++ - GET_X_LPARAM 给出负值

c++ - 如何在 Win32 上读取 wchar_t 缓冲区中的整个文件?

c++ - 关于shared_from_this()实际使用的问题

macos - CMake include_directories 跳过 OS X 框架

c++ - 使用 Ninja 构建系统,我可以清理中间构建产品吗?

c++ - 为什么 if constexpr 不会使这个核心常量表达式错误消失?

c++ - 覆盖参数包声明和扩展位点