"pthread_mutex_t互斥锁= {0}"是否为“初始化互斥锁? [英] Does "pthread_mutex_t mutex = {0}" initialize mutex?

查看:406
本文介绍了"pthread_mutex_t互斥锁= {0}"是否为“初始化互斥锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过这种方式初始化互斥锁:

Is it possible to initialize mutex in this way:

pthread_mutex_t  mutex = {0};

以下3种互斥锁初始化之间有什么区别?

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;

推荐答案

  • 通过第一个选项,您可以通过显式调用初始化函数来控制互斥量的初始化时间(也:参数应为&mutex).
  • 第二个选择是假设有关pthread_mutex_t对象的内部布局的事情,该对象应该是不透明的. 不应使用.
  • 第三个选项静态初始化互斥量.如果在全局或静态范围内定义,它将在程序启动时初始化.也可以在本地范围使用它,但是不建议这样做,因为它不会检查错误情况.
    • 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.
    • 另请参阅: http://pubs.opengroup.org/onlinepubs/7908799 /xsh/pthread_mutex_init.html

      这篇关于"pthread_mutex_t互斥锁= {0}"是否为“初始化互斥锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆