aws-c-common icon indicating copy to clipboard operation
aws-c-common copied to clipboard

Usage of `aws_mutex_init()` vs `AWS_MUTEX_INIT`

Open graebm opened this issue 6 years ago • 1 comments

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:

  1. Only use the macro for statically allocated mutexes?
  2. Always use the macro, and simplify our codebase by removing the aws_mutex_init() and aws_mutex_clean_up() functions.

graebm avatar May 24 '18 18:05 graebm

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?

RandyGaul avatar Jul 10 '18 16:07 RandyGaul