美文网首页
pthread mutexlock

pthread mutexlock

作者: wtqhy14615 | 来源:发表于2017-09-22 18:51 被阅读66次

    在编程中,引入了对象互斥锁的概念,来保证共享数据操作的完整性。每个对象都对应于一个可称为" 互斥锁" 的标记,这个标记用来保证在任一时刻,只能有一个线程访问该对象。

    1. 互斥锁属性

    1. 类型

    参考:https://linux.die.net/man/3/pthread_mutexattr_settype

    1. PTHREAD_MUTEX_DEFAULT: 尝试以递归方式锁定该互斥锁将产生不确定的行为。对于不是由调用线程锁定的互斥锁,如果尝试解除对它的锁定,则会产生不确定的行为。如果尝试解除锁定尚未锁定的互斥锁,则会产生不确定的行为。

    2. PTHREAD_MUTEX_NORMAL: 不提供死锁检测。尝试重新锁定互斥锁会导致死锁。如果某个线程尝试解除锁定的互斥锁不是由该线程锁定或未锁定,则将产生不确定的行为。

    3. PTHREAD_MUTEX_ERRORCHECK: 会提供错误检查。如果某个线程尝试重新锁定的互斥锁已经由该线程锁定,则将返回错误。如果某个线程尝试解除锁定的互斥锁不是由该线程锁定或者未锁定,则将返回错误。

    4. PTHREAD_MUTEX_RECURSIVE: 该互斥锁会保留锁定计数这一概念。线程首次成功获取互斥锁时,锁定计数会设置为1。线程每重新锁定该互斥锁一次,锁定计数就增加 1。线程每解除锁定该互斥锁一次,锁定计数就减小1。 锁定计数达到 0 时,该互斥锁即可供其他线程获取。如果某个线程尝试解除锁定的互斥锁不是由该线程锁定或者未锁定,则将返回错误。

    Note:在iOS系统中,PTHREAD_MUTEX_DEFAULT等于PTHREAD_MUTEX_NORMAL;

    /*
     * Mutex type attributes
     */
    #define PTHREAD_MUTEX_NORMAL        0
    #define PTHREAD_MUTEX_ERRORCHECK    1
    #define PTHREAD_MUTEX_RECURSIVE     2
    #define PTHREAD_MUTEX_DEFAULT       PTHREAD_MUTEX_NORMAL
    

    2. 协议

    参考:https://linux.die.net/man/3/pthread_mutexattr_setprotocol

    When a thread owns a mutex with the PTHREAD_PRIO_NONE protocol attribute, its priority and scheduling shall not be affected by its mutex ownership.

    1. PTHREAD_PRIO_NONE: 当线程拥有这种协议的互斥锁时,该线程的优先级和调度将不受到互斥锁拥有权的影响。

    When a thread is blocking higher priority threads because of owning one or more mutexes with the PTHREAD_PRIO_INHERIT protocol attribute, it shall execute at the higher of its priority or the priority of the highest priority thread waiting on any of the mutexes owned by this thread and initialized with this protocol.

    1. PTHREAD_PRIO_INHERIT: 当拥有一个或多个这种协议属性的互斥锁的线程阻塞更高优先级的线程时,那么该线程将以高于自身或者被该线程所拥有的互斥锁所阻塞线程中最高优先级的线程优先级的运行。

    When a thread owns one or more mutexes initialized with the PTHREAD_PRIO_PROTECT protocol, it shall execute at the higher of its priority or the highest of the priority ceilings of all the mutexes owned by this thread and initialized with this attribute, regardless of whether other threads are blocked on any of these mutexes or not.

    1. PTHREAD_PRIO_PROTECT: 当一个线程拥有这种协议属性的互斥锁时,该线程将以高于其本身优先级或者当前所拥有锁中最高的优先级运行,无论有没有线程被当前所拥有的锁阻塞。
    /*
     * Mutex protocol attributes
     */
    #define PTHREAD_PRIO_NONE            0
    #define PTHREAD_PRIO_INHERIT         1
    #define PTHREAD_PRIO_PROTECT         2
    
    

    3. 共享

    参考:https://linux.die.net/man/3/pthread_mutexattr_setpshared

    iOS只能用PTHREAD_SCOPE_SYSTEM

    /* We only support PTHREAD_SCOPE_SYSTEM */
    #define PTHREAD_SCOPE_SYSTEM         1
    #define PTHREAD_SCOPE_PROCESS        2
    
    #define PTHREAD_PROCESS_SHARED         1
    #define PTHREAD_PROCESS_PRIVATE        2
    
    

    4. 策略

    pthread_mutexattr_setpolicy_np

    /*
     * Mutex attributes
     */
    #define _PTHREAD_MUTEX_POLICY_NONE      0
    #define _PTHREAD_MUTEX_POLICY_FAIRSHARE     1
    #define _PTHREAD_MUTEX_POLICY_FIRSTFIT      2
    

    5. 优先级上限

    参考:https://linux.die.net/man/3/pthread_mutexattr_setprioceiling

    pthread_mutexattr_setprioceiling
    

    使用

        __block NSString *str = @"aa";
        NSLog(@"str: %@", str);
        pthread_mutex_init(&mutexLock, NULL);
        dispatch_queue_t myQueue = dispatch_queue_create("com.investment.concurrent", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(myQueue, ^{
            pthread_mutex_lock(&mutexLock);
            NSLog(@"op1 locked");
            NSLog(@"op1 changing str value euqal to 5");
            sleep(5);
            str = @"bb";
            pthread_mutex_unlock(&mutexLock);
            NSLog(@"op1 execute finished");
            NSLog(@"op1 unlocked");
        });
        dispatch_async(myQueue, ^{
            pthread_mutex_lock(&mutexLock);
            NSLog(@"op2 locked");
            NSLog(@"str: %@", str);
            pthread_mutex_unlock(&mutexLock);
            NSLog(@"op2 unlocked");
        });
        
    
    2017-09-22 18:49:18.133706 iOSLockDemo[15551:4465227] str: aa
    2017-09-22 18:49:18.134384 iOSLockDemo[15551:4465317] op1 locked
    2017-09-22 18:49:18.134448 iOSLockDemo[15551:4465317] op1 changing str value euqal to 5
    2017-09-22 18:49:23.139684 iOSLockDemo[15551:4465317] op1 execute finished
    2017-09-22 18:49:23.139768 iOSLockDemo[15551:4465317] op1 unlocked
    2017-09-22 18:49:23.139797 iOSLockDemo[15551:4465314] op2 locked
    2017-09-22 18:49:23.139880 iOSLockDemo[15551:4465314] str: bb
    2017-09-22 18:49:23.139954 iOSLockDemo[15551:4465314] op2 unlocked
    

    参考

    https://baike.baidu.com/item/互斥锁/841823?fr=aladdin

    相关文章

      网友评论

          本文标题:pthread mutexlock

          本文链接:https://www.haomeiwen.com/subject/mhofextx.html