//
// ViewController.m
// hello
//
// Created by zhaoli on 15/9/30.
// Copyright © 2015年 hello. All rights reserved.
//
#import "ViewController.h"
#import <pthread.h>
@interface ViewController ()
- (IBAction)btnClick;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
* 子线程的任务,执行耗时的操作
*/
void *run(void *data)
{
NSThread *curThread = [NSThread currentThread];
// 耗时操作
for (int i = 0; i < 5000 ; i++) {
NSLog(@"run --- %@",curThread);
}
return NULL;
}
- (IBAction)btnClick {
// 1、获得当前的线程,打印线程
NSThread *curThread = [NSThread currentThread];
NSLog(@"run --- %@",curThread);
// 2、创建一条子线程,执行一些耗时的操作
pthread_t threadID;
/**
* 创建子线程
*
* @param pthread *restrict : 线程的ID,线程创建成功后返回一个ID
* @param const pthread_attr_t *restrict : 线程的一些属性
* @param void * (*)(void *) : 形参和返回值都是void *类型的函数指针,线程
* 开启后要执行的任务
* @param void *restrict :
*
* @return <#return value description#>
*/
pthread_create(&threadID, NULL, run , NULL);
}
本文介绍如何在iOS开发中利用pThread库实现线程并发操作,通过实例展示了如何创建并管理子线程,以及如何在主线程与子线程间进行资源的共享与同步,同时保持UI流畅性。
10万+

被折叠的 条评论
为什么被折叠?



