【问题标题】:Can I use a single "pthread_mutexattr_t" attribute to initialize two different mutexes?我可以使用单个“pthread_mutexattr_t”属性来初始化两个不同的互斥锁吗?
【发布时间】:2013-08-22 09:32:02
【问题描述】:

我在我的 C 程序中使用了两个互斥锁,我希望它们都是以下类型:

PTHREAD_MUTEX_ERRORCHECK

将两个互斥锁的其他属性设置为默认值。

我可以使用单个pthread_mutexattr_t mutexAttrib; 并按照上面设置的“type”属性来初始化两个互斥锁吗?安全吗?

【问题讨论】:

    标签: c pthreads mutex x86-64 suse


    【解决方案1】:

    是的,它是安全的。

    pthread_mutexattr_t属性只在互斥体创建时使用,pthread_mutex_init()不会修改,初始化后也不需要(即你也可以稍后pthread_mutexattr_destroy()它)

    【讨论】:

    • 谢谢!!我还看到here 指针被传递给const pthread_mutexattr_t,所以它不能被pthread_mutex_init() 更改。最初我没有注意到 const.
    猜你喜欢
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多