美文网首页码农的世界
iOS锁-pthread_mutex

iOS锁-pthread_mutex

作者: 口子窖 | 来源:发表于2018-07-13 11:55 被阅读1次

    pthread_mutex_t

    首先是苹果的解释:

    Detects when a mutex is used before it's initialized.

    Overview

    This check detects anytime pthread_mutex_lock(_:) or pthread_mutex_unlock(_:) is called with a pthread_mutex_t variable that wasn't initialized. Attempting to use an uninitialized mutex results in an error, and removes any guarantees about ordering that would exist while a mutex is locked.

    Use of Uninitialized Mutex in C

    In the following example, the pthread_mutex_lock(_:) function is called on an uninitialized pthread_mutex_t variable.

    static pthread_mutex_t mutex;
    void performWork() {
        pthread_mutex_lock(&mutex); // Error: uninitialized mutex
        // ...
        pthread_mutex_unlock(&mutex);
    }
    

    下面是自己的理解和例子代码

    #import <Foundation/Foundation.h>
    @interface NSLockTest : NSObject
    - (void)forTest;
    @end
    
    #import "NSLockTest.h"
    #include <pthread.h>
    @interface NSLockTest()
    @property (nonatomic,strong) NSArray *tickets;
    @property (nonatomic,assign) int soldCount;
    
    @end
    @implementation NSLockTest
    pthread_mutex_t pMutex;
    - (void)forTest
    {
    //    pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
        pthread_mutex_init(&pMutex,NULL);
        self.tickets = @[@"南京-北京A101",@"南京-北京A102",@"南京-北京A103",@"南京-北京A104",@"南京-北京A105",@"南京-北京A106",@"南京-北京A107",@"南京-北京A108",@"南京-北京A109",@"南京-北京A110",@"南京-北京A111",@"南京-北京A112",@"南京-北京A113",@"南京-北京A114",@"南京-北京A115",@"南京-北京A116",@"南京-北京A117",@"南京-北京A118",@"南京-北京A119",@"南京-北京A120",@"南京-北京A121",@"南京-北京A122",@"南京-北京A123",@"南京-北京A124",@"南京-北京A125",@"南京-北京A126",@"南京-北京A127",@"南京-北京A128",@"南京-北京A129",@"南京-北京A130"];
        //第一窗口
        NSThread *windowOne = [[NSThread alloc] initWithTarget:self selector:@selector(soldTicket) object:nil];
        windowOne.name = @"一号窗口";
        [windowOne start];
        //第二窗口
        NSThread *windowTwo = [[NSThread alloc] initWithTarget:self selector:@selector(soldTicket) object:nil];
        windowTwo.name = @"二号窗口";
        [windowTwo start];
        //第三窗口
        NSThread *windowThree = [[NSThread alloc] initWithTarget:self selector:@selector(soldTicket) object:nil];
        windowThree.name = @"三号窗口";
        [windowThree start];
        //第四窗口
        NSThread *windowFour = [[NSThread alloc] initWithTarget:self selector:@selector(soldTicket) object:nil];
        windowFour.name = @"四号窗口";
        [windowFour start];
    }
    -(void)soldTicket
    {
        pthread_mutex_lock(&pMutex);
        if (self.soldCount == self.tickets.count) {
            NSLog(@"=====%@ 剩余票数:%lu",[[NSThread currentThread] name],self.tickets.count-self.soldCount);
            pthread_mutex_unlock(&pMutex);
            return;
        }
        //延时卖票
        [NSThread sleepForTimeInterval:0.2];
        self.soldCount++;
        NSLog(@"=====%@ %@ 剩%lu",[[NSThread currentThread] name],self.tickets[self.soldCount-1],self.tickets.count-self.soldCount);
        pthread_mutex_unlock(&pMutex);
        //一直卖票
        [self soldTicket];
        
    }
    @end
    
    

    注意

    1、申明一个互斥锁,pthread_mutex_t pMutex;

    2、初始化它,pthread_mutex_init(&pMutex,NULL);

    3、使用pMutex之前一定要初始化,否则不生效

    4、获得锁,pthread_mutex_lock(&pMutex);

    5、解锁,pthread_mutex_unlock(&pMutex);

    相关文章

      网友评论

        本文标题:iOS锁-pthread_mutex

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