简体   繁体   English

“pthread_mutex_t mutex = {0}”初始化互斥锁吗?

[英]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: 以下3个互斥锁初始化有什么区别:

1) pthread_mutex_init(&mutex, NULL);
2) pthread_mutex_t  mutex = {0};
3) pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;
  • 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. 使用第一个选项,您可以通过显式调用初始化函数来控制互斥锁初始化的时间(同样:参数应为&mutex )。
  • The second option is assuming things about the internal layout of the pthread_mutex_t object, which is supposed to be opaque. 第二个选项是假设pthread_mutex_t对象的内部布局,它应该是不透明的。 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 另见: http//pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutex_init.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM