The function pthread_mutex_init()returns a nonzero value when the mutex cannot be initialized, and the class std::mutexin C ++ 11 has a constructor noexcept.
Suppose you decide to implement the mutex C ++ class on top of mutex pthreads. It completes the mutex pthread inside the class and tries to initialize it by calling pthread_mutex_init () in the constructor. If the function call returns a non-zero value, that is, an error, the error cannot be reported immediately, since the constructor cannot throw. One option is to throw an exception until the lock method is actually called by the mutex. But this approach seems wrong.
Is there any other way to do this using some clever tricks to ensure that initializing the mutex will always be successful?
Update: I am going to answer my question on this. According to the locale , in 30.4.1.3 pge 1163 it says: "If the initialization of an object of type mutex fails, an exception of type system_error will be thrown."
And the noexcept function can call functions inside the body, but only the caller cannot catch the exception. If an exception is thrown inside the noexcept function, std :: terminate will be called.
source
share