I'm reading the source code of call_once
in libc++, and curious about the usage of a shared mutex. Here's the implementation inside mutex.cpp
. Doesn't this mean call_once(f1)
and call_once(f2)
compete for the same mutex even if they are different functions. Thanks.
#ifndef _LIBCPP_HAS_NO_THREADS
_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
_LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
#endif
void __call_once(volatile once_flag::_State_type& flag, void* arg,
void (*func)(void*)) {
__libcpp_mutex_lock(&mut);
...
}
I'm hoping someone one can clarify if this is intended, and if so what's the reason behind.