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
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.