内容简介:pthread_create 即为使用C语言的方式创建一条线程注意:停止线程时,我们必须先将该线程的任务停止下来,即使循环停止,所以在暂停与恢复线程的原理即利用
需求:iOS封装C语言P Thread以实现开始,结束,暂停,继续,指定线程名称,任务等的需求。
阅读前提:
- 了解p thread基本用法
- 了解iOS端线程基本概念
- 了解线程加锁基本原理
GitHub地址(附代码) : iOS封装C语言P Thread
简书地址 : iOS封装C语言P Thread
博客地址 :iOS封装C语言P Thread
掘金地址 : iOS封装C语言P Thread
一. 基类的写法
1.所需变量
@interface XDXPThreadHandler ()
{
pthread_t _baseThread; // 即我们主要控制的线程
bool _isStopBaseThread; // 是否停止线程的标志
bool _isPauseBaseThread; // 是否暂停线程的标志
pthread_mutex_t _baseThreadMutex; // 线程的锁
pthread_cond_t _baseThreadCond; // 暂停与唤醒线程的变量
}
@end
2.初始化
- (void)initBaseThread {
_isStopBaseThread = false;
_isPauseBaseThread = false;
pthread_mutex_init(&_baseThreadMutex, NULL);
pthread_cond_init(&_baseThreadCond, NULL);
}
3.开启线程
pthread_create 即为使用 C语言 的方式创建一条线程
- 第一个参数 : 创建的线程对象
- 第二个参数 : 属性,可填空
- 第三个参数 : 该线程创建完成后触发的方法(C语言的函数,非OC的方法),即该方法内即为在该线程内
-
第四个参数 : 向第三个方法中传递的参数(我们在这里需要将该类的对象的实例传过去,否则在函数内无法调用本类相关的方法和属性)
int pthread_create(pthread_t * __restrict, const pthread_attr_t * _Nullable __restrict, void *(* _Nonnull)(void *), void * _Nullable __restrict);
- (void)startBaseThreadTask {
[self initBaseThread];
pthread_create(&_baseThread, NULL, doBaseThreadTask, (__bridge_retained void *)self);
log4cplus_error("XDXPThreadHandler", "%s - Start send BaseThread info thread !",ModuleName);
}
void * doBaseThreadTask(void *param); // 函数声明
void * doBaseThreadTask(void *param) {
XDXPThreadHandler *instance = (__bridge_transfer XDXPThreadHandler *)param;
pthread_setname_np(instance.baseThreadName.UTF8String);
while (instance->_isStopBaseThread == false) {
if (true == instance->_isPauseBaseThread) {
pthread_mutex_lock(&instance->_baseThreadMutex);
pthread_cond_wait(&instance->_baseThreadCond,&instance->_baseThreadMutex);
pthread_mutex_unlock(&instance->_baseThreadMutex);
}else {
[instance doBaseThreadTask];
usleep(XDXUSleepOneSec);
}
}
return NULL;
}
- (void)doBaseThreadTask {
}
- pthread_setname_np : 设置线程的名称
线程解析
- 因为我们要模拟类似OC NSTimer每隔多少秒执行一次任务,即我们需要一个while循环不断执行,执行一次sleep指定时间
- 其次我们需要通过是否停止flag来控制是否需要退出循环,好让其他线程能够销毁掉此线程(调用pthread_join的原理为等线程结束任务后将线程杀掉)
- 如果需要暂停线程,我们需要借助pthread_cond_wait函数来暂停线程,注意暂停恢复操作需要加锁处理
- 如果不需要上述两个flag我们则进行线程正常的操作,每隔指定秒数循环一次,这样子类只需要重写doBaseThreadTask即可使用该线程
4.停止线程
注意:停止线程时,我们必须先将该线程的任务停止下来,即使循环停止,所以在 - (void)freeResource
中我们先将 _isStopBaseThread = true;
- pthread_join : 即为等待线程任务结束后,杀死指定线程。 注意此函数只能在非自身线程执行!
-
_baseThread = NULL;此操作必须在最后执行,否则无法执行pthread_join函数
- (void)stopBaseThreadTaskThread {
if (_baseThread) {
log4cplus_error("XDXPThreadHandler", "%s - Stop send BaseThread thread !",ModuleName);
[self freeResource];
}else {
log4cplus_error("XDXPThreadHandler", "%s - Stop send BaseThread thread Failed, The base thread was destoryed!",ModuleName);
}
}
- (void)freeResource {
_isStopBaseThread = true;
pthread_mutex_destroy(&_baseThreadMutex);
pthread_cond_destroy(&_baseThreadCond);
int err = pthread_join(_baseThread, NULL);
if (err != 0) {
log4cplus_error("XDXPThreadHandler", "%s - Destory send BaseThread thread faild. status : %d",ModuleName,err);
}else {
log4cplus_error("XDXPThreadHandler", "%s - Destory send BaseThread thread !",ModuleName);
}
_baseThread = NULL;
_isStopBaseThread = false;
_isPauseBaseThread = false;
log4cplus_error("XDXPThreadHandler", "%s - Free send BaseThread info thread !",ModuleName);
}
5.暂停与恢复线程
暂停与恢复线程的原理即利用 _isPauseBaseThread
该flag让线程执行暂停与执行所对应的函数
- pthread_cond_broadcast : 唤醒某条线程
- pthread_cond_wait : 使某条线程sleep
- (void)pauseBaseThread {
if (_isPauseBaseThread == false) {
pthread_mutex_lock(&_baseThreadMutex);
_isPauseBaseThread = true;
pthread_mutex_unlock(&_baseThreadMutex);
log4cplus_info("Crop", "Suspend send BaseThread info Thread !");
}else {
log4cplus_error("Crop", "The send BaseThread info thread had Suspend!");
}
}
- (void)continueBaseThread {
if (_isPauseBaseThread == true) {
pthread_mutex_lock(&_baseThreadMutex);
_isPauseBaseThread = false;
pthread_cond_broadcast(&_baseThreadCond);
pthread_mutex_unlock(&_baseThreadMutex);
log4cplus_info("Crop", "Resume send BaseThread info Thread !");
}else {
log4cplus_error("Crop", "The send BaseThread info Thread is running!");
}
}
二. 子类继承基类
有了第一步中的操作,我们的父类线程已经写好,子类只要继承父类并实现 - (void)doBaseThreadTask
即可做到每隔指定秒数完成某项任务
@interface XDXTestPThreadHandler : XDXPThreadHandler
- (void)setBaseThreadName:(NSString *)name;
@end
@implementation XDXTestPThreadHandler
- (void)doBaseThreadTask {
[super doBaseThreadTask];
NSLog(@"Hello");
}
@end
三. 程序中调用
我们在主程序中可以设置线程名称以及线程每次等待时间等等,然后调用start,stop,pause,continue即可看到控制台上关于线程的打印,证明线程的功能已经实现完毕。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 非单例
self.testThread = [[XDXTestPThreadHandler alloc] init];
self.testThread.baseThreadName = @"World";
// 单例
self.testAThread= [XDXTestAPThreadHandler getInstance];
self.testAThread.baseThreadName = @"Hello";
}
#pragma mark test
- (IBAction)startBtnClicked:(id)sender {
[self.testThread startBaseThreadTask];
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Go语言同步与异步执行多个任务封装详解(Runner和RunnerAsync)
- 封装JDBC—非框架开发必备的封装类
- SpringBlade 2.3.2 发布,增加 OSS 封装及单元测试封装
- SpringBlade 2.3.2 发布,增加 OSS 封装及单元测试封装
- docker 封装 alinode
- 封装Apk签名工具
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Unity游戏设计与实现
[日]加藤政树 / 罗水东 / 人民邮电出版社 / 2015-2 / 79.00元
本书出自日本知名游戏公司万代南梦宫的资深开发人员之手,面向初级游戏开发人员,通过10个不同类型的游戏实例,展示了真正的游戏设计和实现过程。本书的重点并不在于讲解Unity的各种功能细节,而在于核心玩法的设计和实现思路。每个实例都从一个idea 开始,不断丰富,自然而然地推出各种概念,引导读者思考必要的数据结构和编程方法。掌握了这些思路,即便换成另外一种引擎,也可以轻松地开发出同类型的游戏。 ......一起来看看 《Unity游戏设计与实现》 这本书的介绍吧!