Does “pthread_mutex_t mutex = {0}” initialize mutex?

只谈情不闲聊 提交于 2019-12-11 00:22:02

问题


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;

回答1:


  • 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



来源:https://stackoverflow.com/questions/14622646/does-pthread-mutex-t-mutex-0-initialize-mutex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!