我们在ios的线程使用中
很少会使用pthread 的线程
因为苹果封装了更加高阶的nsthread opeationqueue
还有GCD
但pthread 由他独特的跨平台性
和简单的api调用而突出
ios平台的相关博客
很少会讲到pthread 的用法
这里用一个简单的例子,来将c 的线程
线程锁,条件锁一次性讲清楚
题目很简单,就是一个工厂的生产者在生产苹果
一个消费者在一直消费苹果,就用代码来实现这个过程
显然 生产者是一个线程,消费者是一个线程
所以需要初始化这两个线程
首先引入头文件
#include <pthread.h>
接下来写生产者线程
pthread_t productThread; //声明生产者线程对象
pthread_create(&productThread,NULL,
productThreadCallBack,NULL);//创建生产者线程
//第一个参数传你声明的那个线程 第二第四可以不填 第三个填一个线程执行函数
//执行函数,注意线程完成需要退出线程
void *productThreadCallBack(void *data) {
printf("productThreadCallBack thread----\n");
pthread_exit(&productThread);//退出生产者线程
}
以上就四步1.声明线程对象2.创建一个线程执行函数 3.创建并开始线程 4.退出线程 就是一个线程的过程了
同理 消费者的线程
pthread_t customerThread; //声明消费者线程对象
pthread_create(&customerThread,NULL,
customerThreadCallBack,NULL);//创建消费者线程
//执行函数
void *customerThreadCallBack(void *data) {
printf("customerThreadCallBack thread----\n");
pthread_exit(&customerThread);//退出消费者线程
}
到这里我们已经完成了生产者和消费者的线程创建
实际上我们没有在线程里面写任何代码了 只各自写了一个打印函数
那么我们要在生产者线程productThreadCallBack
和消费者线程的执行函数customerThreadCallBack
里面写代码
生产者里面就是写生产苹果
消费者里面就是写消费苹果
这里我们就需要引入一个变量 苹果总数
@property(nonatomic, strong) NSMutableArray *dataArray; //数据源 生产总数
这里我没用C++的queue 队列 是为了给ios开发者更容易理解
首先先初始化数据源并且让工厂首先先有10个苹果
self.dataArray = [NSMutableArray array];
for (NSInteger i=0; i<10; i++) {
[self.dataArray addObject:@"an apple"];
}
对于生产者线程来说
每生产一个苹果,总数量就会加1
[self.dataArray addObject:@"an apple"];
对于消费者线程来说
每消费一个苹果,总数量就会少1
if (self.dataArray.count >0) {
[self.dataArray removeLastObject];
}
而生产和消费是一个永不停止的过程
所以就加上了while循环
生产者
while (1) {
[self.dataArray addObject:@"an apple"];
sleep(5);//睡眠5秒
}
消费者
while (1) {
if (self.dataArray.count >0) {
[self.dataArray removeLastObject];
}
sleep(1);//睡眠1秒
}
可以看到生产者5秒生产一个苹果
消费者1秒消费一个苹果
如果说有出现一种特殊的情况 在生产者还没把苹果添加成功
但是总个数已经变成1 实际上总个数还是0 的情况下调用了
[self.dataArray removeLastObject];
会导致数组越界的崩溃,这就是由于资源竞争引起的问题
因为生产者和消费者同时在访问一个资源造成的
因此就出现了加锁来解决这个问题
加锁的原理类似于上厕所,一个人进入厕所,就会把厕所门关上
加锁,其他人就无法再访问这个坑的资源了,
只有他使用完毕了厕所 ,打开厕所门,解锁之后,另外一个人才能够
访问厕所了
线程锁的代码
pthread_mutex_t mute;//声明一个线程锁对象
pthread_mutex_init(&mute, NULL);//初始化锁
pthread_mutex_lock(&mute);//加锁
pthread_mutex_unlock(&mute);//解锁
那么对于消费者 加锁就写在生产苹果的过程中,生产过程中
别人不能访问这个总苹果树资源
while (1) {
pthread_mutex_lock(&mute);//加锁
[self.dataArray addObject:@"an apple"];
pthread_mutex_unlock(&mute);//解锁
sleep(5);//睡眠5秒
}
对于消费者来说 就是他在消费的过程中,别人不能访问这个总苹果资源
while (1) {
pthread_mutex_lock(&mute);//加锁
if (self.dataArray.count >0) {
[self.dataArray removeLastObject];
}
pthread_mutex_unlock(&mute);//解锁
sleep(1);//睡眠1秒
}
这样写就不会造成资源竞争了
那么条件锁又是做什么的呢?
注意到了么消费者的函数里面有个判断函数
if (self.dataArray.count >0)
我们知道 生产者 5秒生产一个苹果
消费者1秒消费一个苹果
很快就会遇到没苹果的情况 就是判断条件不满足的情况
这时候while循环还是一遍一遍的问这个判断条件就
出现了资源浪费的情况
我们有一种办法 就是
当消费者没有苹果可以吃的时候,
就让消费者一直等待
等待有人告诉他 不需要等待了
有苹果吃了就又开始吃苹果的循环
这样的等待 和发送消息 的事件 就是条件锁的使用
一开始是处于等待状态
当生产了一个苹果的时候,
就会发送给条件锁 现在你不用等了
可以继续执行了
条件锁的函数
pthread_cond_t cond;//声明一个线程条件锁对象
pthread_cond_init(&cond,NULL);//初始化条件锁
pthread_cond_wait(&cond, &mute);//等待条件锁发送信号,收到信号可以解锁线程的锁对象
pthread_cond_signal(&cond);//发送信号
那么消费者的使用 处于等待状态
while (1) {
pthread_mutex_lock(&mute);//加锁
if (self.dataArray.count >0) {
[self.dataArray removeLastObject];
}else {
pthread_cond_wait(&cond, &mute);//等待条件锁发送信号,收到信号可以解锁
}
pthread_mutex_unlock(&mute);//解锁
sleep(1);//睡眠1秒
}
生产者的使用
while (1) {
pthread_mutex_lock(&mute);//加锁
[self.dataArray addObject:@"an apple"];
pthread_cond_signal(&cond);//发送信号
pthread_mutex_unlock(&mute);//解锁
sleep(5);//睡眠5秒
}
这样就可以减少消费者 的while循环 从而减少内存的消耗
到这里就全部写完了
还有一个注意点是要将C语言的函数转换成oc的方法调用
例如
id object; //用于将c的方法转为oc 的方法
object = self;//自身赋给静态object 用于c 方法中转为oc 方法
#pragma mark - 消费者线程
void *customerThreadCallBack(void *data) {
printf("customerThreadCallBack thread----\n");
[object customerThead];
pthread_exit(&customerThread);//退出消费者线程
}
- (void)customerThead {
}
最终完成版的代码是这样的
//
// ViewController.m
// Cthread
//
// Created by kai_leedarson on 2018/11/5.
// Copyright © 2018年 maple_leedarson. All rights reserved.
//
#import "ViewController.h"
#include <pthread.h>
@interface ViewController ()
@property(nonatomic, strong) NSMutableArray *dataArray; //数据源 生产总数
@end
id object; //用于将c的方法转为oc 的方法
@implementation ViewController
pthread_t productThread; //声明生产者线程对象
pthread_t customerThread; //声明消费者线程对象
pthread_mutex_t mute;//声明一个线程锁对象
pthread_cond_t cond;//声明一个线程条件锁对象
bool threadExit = false; //控制while循环退出的bool值
#pragma mark - 生产者线程
void *productThreadCallBack(void *data) {
printf("productThreadCallBack thread----\n");
[object productThread];
pthread_exit(&productThread);//退出生产者线程
}
- (void)productThread {
if (!self.dataArray) {
NSLog(@"数据源没有初始化过");
return;
}
while (!threadExit) {
pthread_mutex_lock(&mute);//加锁
[self.dataArray addObject:@"an apple"];
NSLog(@"生产了一个苹果 并发送信号给消费者 当前生产苹果总数 %lu",(unsigned long)self.dataArray.count);
pthread_cond_signal(&cond);//发送信号
pthread_mutex_unlock(&mute);//解锁
sleep(5);//睡眠5秒
}
}
#pragma mark - 消费者线程
void *customerThreadCallBack(void *data) {
printf("customerThreadCallBack thread----\n");
[object customerThead];
pthread_exit(&customerThread);//退出消费者线程
}
- (void)customerThead {
if (!self.dataArray) {
NSLog(@"数据源没有初始化过");
return;
}
while (!threadExit) {
pthread_mutex_lock(&mute);//加锁
if (self.dataArray.count >0) {
[self.dataArray removeLastObject];
NSLog(@"消费了一个苹果 当前生产苹果总数 %lu",(unsigned long)self.dataArray.count);
}else {
NSLog(@"当前已经没有苹果了 持续等待中 %lu",(unsigned long)self.dataArray.count);
pthread_cond_wait(&cond, &mute);//等待条件锁发送信号,收到信号可以解锁
}
pthread_mutex_unlock(&mute);//解锁
sleep(1);//睡眠1秒
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dataArray = [NSMutableArray array];
for (NSInteger i=0; i<10; i++) {
[self.dataArray addObject:@"an apple"];
}
object = self;//自身赋给静态object 用于c 方法中转为oc 方法
pthread_mutex_init(&mute, NULL);//初始化锁
pthread_cond_init(&cond,NULL);//初始化条件锁
NSLog(@"viewDidLoad");
}
- (void)threadStart {
pthread_create(&productThread,NULL,
productThreadCallBack,NULL);//创建生产者线程
pthread_create(&customerThread,NULL,
customerThreadCallBack,NULL);//创建消费者线程
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self threadStart];
}
@end