aws-c-common
aws-c-common copied to clipboard
Usage of `aws_mutex_init()` vs `AWS_MUTEX_INIT`
There are cases in our code of AWS_MUTEX_INIT
being used to initialize a mutex within a function.
Documentation for PTHREAD_MUTEX_INITIALIZER says that the macro is intended for use with static variables. Likewise with the SRWLOCK_INIT macro on Windows.
However, libc++'s implementation of std::mutex
uses the macro in its constructor, and the constructor is noexcept
.
Googling around, I found this blog post about implementing std::mutex
for embedded systems, and how the author needed to remove the noexcept
from the constructor because it could throw.
So should we:
- Only use the macro for statically allocated mutexes?
- Always use the macro, and simplify our codebase by removing the
aws_mutex_init()
andaws_mutex_clean_up()
functions.
Reading the pthread docs, sounds like the static initializer is only useful for default settings/attributes, and it skips error checking.
Why not just use appropriate initializer functions, and avoid the initializer macro entirely?