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

前端 未结 1 1318
渐次进展
渐次进展 2020-12-10 16:08

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.

#i         


        
相关标签:
1条回答
  • 2020-12-10 16:15

    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.

    0 讨论(0)
提交回复
热议问题