Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "pthread_mutex_t mutex = {0}" initialize mutex?

Is it possible to initialize mutex in this way:

pthread_mutex_t  mutex = {0};

What is the difference between the following 3 initialization of mutex:

1) pthread_mutex_init(&mutex, NULL);
2) pthread_mutex_t  mutex = {0};
3) pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;
like image 955
MOHAMED Avatar asked Jun 11 '26 13:06

MOHAMED


1 Answers

  • With the first option, you control the time at which the mutex is initialized (also: the argument should be &mutex) by calling the initializer function explicitly.
  • The second option is assuming things about the internal layout of the pthread_mutex_t object, which is supposed to be opaque. It should not be used.
  • The third option initializes the mutex statically. If defined at global or static scope, it will be initialized at program startup. It can be used also at local scope, but this is not recommended, as it does not check for error conditions.

See also: http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutex_init.html

like image 87
sheu Avatar answered Jun 14 '26 03:06

sheu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!