pthread_mutex_t 互斥锁

如果多个线程要同时访问(读和写)一个资源,资源的同步性和有效性就不可控。
比如以下代码

@implementation Thread1 {
    pthread_t _thread0;
    pthread_t _thread1;
}

void* thread_func(void *arg) {
    for (int i = 0; i < 10; i++) {
        printf("%d\n",i);
    }
    return NULL;
}

void *thread_func1(void *arg) {
    for (int i = 0; i < 10; ++i) {
        printf("       %d\n",i);
    }
    return NULL;
}

-(void) initializeThead {    
    pthread_mutex_init(&_mutex, NULL);

    pthread_create(&_thread0, NULL, thread_func, NULL);
    pthread_create(&_thread1, NULL, thread_func1, NULL);
}
@end

输出结果就不可控,如下:

0
       0
1
       1
2
       2
3
       3
4
       4
5
       5
6
       6
7
       7
8
       8
9
       9
0
1
2
3
4
5
6
7
8
9
       0
       1
       2
       3
       4
       5
       6
       7
       8
       9
       0
0
       1
1
       2
2
       3
3
       4
4
       5
5
       6
6
       7
7
       8
8
       9
9
0
1
2
       0
3
       1
4
       2
5
       3
6
       4
7
       5
8
       6
9
       7
       8
       9
      1. ... ...

那怎么办?

可以加个“互斥锁”pthread_mutex_t

当一个线程A先加上了“锁”时,另一个同样想加“锁”的线程B就只能先等待了。等到什么时候呢,等到那个先加上“锁”的线程A,“解锁”后,这个线程B可以“抢”到“锁”,然后执行代码。这里说的是“抢”,万一有个线程C,又先抢到了“锁”,那线程B只能再等了,就是传说中的插队了。
代码以下:

@implementation Thread1 {
    pthread_t _thread0;
    pthread_t _thread1;
}
pthread_mutex_t _mutex;

void* thread_func(void *arg) {
    pthread_mutex_lock(&_mutex);
    for (int i = 0; i < 10; i++) {
        printf("%d\n",i);
    }
    pthread_mutex_unlock(&_mutex);
    return NULL;
}

void *thread_func1(void *arg) {
    pthread_mutex_lock(&_mutex);
    for (int i = 0; i < 10; ++i) {
        printf("       %d\n",i);
    }
    pthread_mutex_unlock(&_mutex);
    return NULL;
}

-(void*) yteue:(void *) arg {
    return NULL;
}

-(void) initializeThead {
    pthread_mutex_init(&_mutex, NULL);
    
    pthread_create(&_thread0, NULL, thread_func, NULL);
    pthread_create(&_thread1, NULL, thread_func1, NULL);
}

@end

这段就只有两个情况,要么A先抢到锁B等,要么B先抢到锁A等。

       0
       1
       2
       3
       4
       5
       6
       7
       8
       9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
       0
       1
       2
       3
       4
       5
       6
       7
       8
       9

有很大的进步了。

那我就是狮子座,就是想先让A执行,再进行B。

怎么办?

pthread_cond_t来了cond 应该就是condition的意思了。加条件嘛〜〜〜

一般来说"锁"和“条件”是配合来使用的,比如说,线程A抢到锁了,发现我这个主人不想让它执行(不符合条件),只能先等等喽(能有什么办法!),先让别人执行,但锁还是我手里啊。别人眼巴巴的看着线程A手里的锁,又不能执行。没办法,先扔出去,线程A在这里等条件完成的通知。通知到了(条件符合了),就把锁抢回来,继续执行线程A的代码。
代码如下:

@implementation Thread1 {
    pthread_t _thread0;
    pthread_t _thread1;
}

bool _flag;
pthread_mutex_t _mutex;
pthread_cond_t _cond;

void* thread_func(void *arg) {
    pthread_mutex_lock(&_mutex);
    while (_flag) {
        printf("等\n");
        //这里就是扔锁,等通知的,然后通知到了,再加锁的代码。
        pthread_cond_wait(&_cond, &_mutex);
    }
    for (int i = 0; i < 10; i++) {
        printf("%d\n",i);
    }
    pthread_mutex_unlock(&_mutex);
    return NULL;
}

void *thread_func1(void *arg) {
    pthread_mutex_lock(&_mutex);
    for (int i = 0; i < 10; ++i) {
        printf("       %d\n",i);
    }
    _flag = false;
    pthread_cond_signal(&_cond);
    pthread_mutex_unlock(&_mutex);
    return NULL;
}

-(void) initializeThread {
    pthread_mutex_init(&_mutex, NULL);
    pthread_cond_init(&_cond, NULL);
    _flag = true;
    
    pthread_create(&_thread0, NULL, thread_func, NULL);
    pthread_create(&_thread1, NULL, thread_func1, NULL);
}
@end

这里也是两种结果:

等
       0
       1
       2
       3
       4
       5
       6
       7
       8
       9
0
1
2
3
4
5
6
7
8
9
       0
       1
       2
       3
       4
       5
       6
       7
       8
       9
0
1
2
3
4
5
6
7
8
9

有“等”字的说明,另一个线程先抢到锁了,但只能“等”喽了。

如果出现一种情况,同时有两个线程在“等”,

会是怎么样的?

如果有两个线程B和C,同时在“等”,A用pthread_cond_signal放出一个“别等啦”的信号,会是怎么样的?

@implementation Thread1 {
    pthread_t _thread0;
    pthread_t _thread1;
    pthread_t _thread2;
}

bool _flag;
pthread_mutex_t _mutex;
pthread_cond_t _cond;

void* thread_func(void *arg) {
    pthread_mutex_lock(&_mutex);
    while (_flag) {
        printf("等1\n");
        //这里就是扔锁,等通知的,然后通知到了,再加锁的代码。
        pthread_cond_wait(&_cond, &_mutex);
    }
    for (int i = 0; i < 10; i++) {
        printf("A:%d\n",i);
    }
    _flag = true;
    pthread_mutex_unlock(&_mutex);
    return NULL;
}

void *thread_func1(void *arg) {
    pthread_mutex_lock(&_mutex);
    for (int i = 0; i < 10; ++i) {
        printf("       %d\n",i);
    }
    _flag = false;
    pthread_cond_signal(&_cond);
    pthread_mutex_unlock(&_mutex);
    return NULL;
}

void* thread_func2(void *arg) {
    pthread_mutex_lock(&_mutex);
    while (_flag) {
        printf("等2\n");
        //这里就是扔锁,等通知的,然后通知到了,再加锁的代码。
        pthread_cond_wait(&_cond, &_mutex);
    }
    for (int i = 0; i < 10; i++) {
        printf(" B:%d\n",i);
    }
    pthread_mutex_unlock(&_mutex);
    return NULL;
}


-(void*) yteue:(void *) arg {
    return NULL;
}

-(void) initializeThead {
    pthread_mutex_init(&_mutex, NULL);
    pthread_cond_init(&_cond, NULL);
    _flag = true;
    
    pthread_create(&_thread2, NULL, thread_func2, NULL);
    pthread_create(&_thread0, NULL, thread_func, NULL);
    pthread_create(&_thread1, NULL, thread_func1, NULL);
}
@end

出现以下两种结果:

等1
等2
       0
       1
       2
       3
       4
       5
       6
       7
       8
       9
A:0
A:1
A:2
A:3
A:4
A:5
A:6
A:7
A:8
A:9
等2
等1
       0
       1
       2
       3
       4
       5
       6
       7
       8
       9
 B:0
 B:1
 B:2
 B:3
 B:4
 B:5
 B:6
 B:7
 B:8
 B:9

嗯?好像只是让先“等”的那条线程“别等啦”,但,后等的那个好像还在“等”阿。傻了吧。

怎么办!!

这时就要用pthread_cond_broadcast广播啦,这个不像 pthread_cond_signal只触发一个“等”,broadcast是广播的意思,就是让所有用这个“_cond”在等待的线程都激活。然后所有的被激活的线程又重新去抢“锁”。
代码如下:

@implementation Thread1 {
    pthread_t _thread0;
    pthread_t _thread1;
    pthread_t _thread2;
}

bool _flag;
pthread_mutex_t _mutex;
pthread_cond_t _cond;

void* thread_func(void *arg) {
    pthread_mutex_lock(&_mutex);
    while (_flag) {
        printf("等1\n");
        //这里就是扔锁,等通知的,然后通知到了,再加锁的代码。
        pthread_cond_wait(&_cond, &_mutex);
    }
    for (int i = 0; i < 10; i++) {
        printf("A:%d\n",i);
    }
    pthread_mutex_unlock(&_mutex);
    return NULL;
}

void *thread_func1(void *arg) {
    pthread_mutex_lock(&_mutex);
    for (int i = 0; i < 10; ++i) {
        printf("       %d\n",i);
    }
    _flag = false;
    pthread_cond_broadcast(&_cond);
    pthread_mutex_unlock(&_mutex);
    return NULL;
}

void* thread_func2(void *arg) {
    pthread_mutex_lock(&_mutex);
    while (_flag) {
        printf("等2\n");
        //这里就是扔锁,等通知的,然后通知到了,再加锁的代码。
        pthread_cond_wait(&_cond, &_mutex);
    }
    for (int i = 0; i < 10; i++) {
        printf(" B:%d\n",i);
    }
    pthread_mutex_unlock(&_mutex);
    return NULL;
}


-(void*) yteue:(void *) arg {
    return NULL;
}

-(void) initializeThead {
    pthread_mutex_init(&_mutex, NULL);
    pthread_cond_init(&_cond, NULL);
    _flag = true;
    
    pthread_create(&_thread2, NULL, thread_func2, NULL);
    pthread_create(&_thread0, NULL, thread_func, NULL);
    pthread_create(&_thread1, NULL, thread_func1, NULL);
}
@end

结果,大致有两种(没有“等”的结果就先去掉,没有意义):

等2
等1
       0
       1
       2
       3
       4
       5
       6
       7
       8
       9
 B:0
 B:1
 B:2
 B:3
 B:4
 B:5
 B:6
 B:7
 B:8
 B:9
A:0
A:1
A:2
A:3
A:4
A:5
A:6
A:7
A:8
A:9
等1
等2
       0
       1
       2
       3
       4
       5
       6
       7
       8
       9
A:0
A:1
A:2
A:3
A:4
A:5
A:6
A:7
A:8
A:9
 B:0
 B:1
 B:2
 B:3
 B:4
 B:5
 B:6
 B:7
 B:8
 B:9
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,176评论 5 469
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,190评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,232评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,953评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,879评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,177评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,626评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,295评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,436评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,365评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,414评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,096评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,685评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,771评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,987评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,438评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,032评论 2 341

推荐阅读更多精彩内容