what is the “attribute” of a pthread mutex?

后端 未结 4 434
暗喜
暗喜 2020-12-14 07:11

The function pthread_mutex_init allows you to specify a pointer to an attribute. But I have yet to find a good explanation of what pthread attributes are. I have always just

相关标签:
4条回答
  • 2020-12-14 07:26

    All mutex attributes are set in a mutex attribute object by a function of the form:

    int pthread_mutexattr_setname(pthread_attr_t *attr, Type t);
    

    All mutex attributes are retrieved from a mutex attribute object by a function of the form:

    int pthread_mutexattr_getname(const pthread_attr_t *attr, Type *t);
    

    where name and Type are defined as in the table below:

    Type and Name   Description and Value(s)
    int protocol    Define the scheduling classes for mutex locks 
                    PTHREAD_PRIO_NONE,PTHREAD_PRIO_PROTECT,
                    PTHREAD_PRIO_INHERIT
    
    int pshared Defines whether a mutex is shared with other processes. 
                    PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_PRIVATE
    
    int prioceiling Used for mutex attribute priority ceiling values. 
                    See POSIX.1 section 13
    
    int type    Application defined mutex locking
                    PTHREAD_MUTEX_NORMAL,PTHREAD_MUTEX_RECURSIVE,
                    PTHREAD_MUTEX_ERRORCHECK,PTHREAD_MUTEX_DEFAULT
    
    0 讨论(0)
  • 2020-12-14 07:28

    Applying NULL to this argument implies using the default argument. So for some reasons you could want to change these default settings (using pthread_mutexattr_init).

    The documentation explains all you need about these mutex settings.

    0 讨论(0)
  • 2020-12-14 07:34

    If you scroll down the function listing for <pthread.h>, you will find a bunch of pthread_mutexattr_... functions, including an init, destroy and functions to set various attributes of a mutex. When you pass NULL, the mutex is created with suitable defaults for all these attributes, but if you need to modify specific attributes, you can construct a pthread_mutexattr_t structure and pass it in.

    0 讨论(0)
  • 2020-12-14 07:37

    The best place to find that information is from the POSIX standards pages.

    A NULL mutex attribute gives you an implementation defined default attribute. If you want to know what you can do with attributes, check out the following reference and follow the pthread_mutexattr_* links in the SEE ALSO section. Usually, the default is a sensible set of attributes but it may vary between platforms, so I prefer to explicitly create mutexes with known attributes (better for portability).

    This is for issue 7 of the standard, 1003.1-2008. The starting point for that is here. Clicking on Headers in the bottom left will allow you to navigate to the specific functionality (including pthreads.h).

    The attributes allow you to set or get:

    • the type (deadlocking, deadlock-detecting, recursive, etc).
    • the robustness (what happens when you acquire a mutex and the original owner died while possessing it).
    • the process-shared attribute (for sharing a mutex across process boundaries).
    • the protocol (how a thread behaves in terms of priority when a higher-priority thread wants the mutex).
    • the priority ceiling (the priority at which the critical section will run, a way of preventing priority inversion).

    And, for completeness, there's the init and destroy calls as well, not directly related to a specific attribute but used to create them.

    0 讨论(0)
提交回复
热议问题