iOS多线程之pthread

pthread是iOS多线程底层的实现,平时开发使用平率极少,下面做一个简单的介绍:

#import "ViewController.h"
#import <pthread.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    pthread_t thread;//定义一个pthread_t类型的结构体变量
    void * (*funp)(void *) = fun; //定义void *(*)(void *)类型的函数指针
    pthread_create(&thread, NULL, funp, NULL);//用过调用pthread_create,传入传入thread指针参数创建线程,传入函数指针指定要执行的代码块

}

/** * pthread_create 回调函数,通过此函数作为子线程的执行的代码块 * *  @param param <#param description#> * *  @return <#return value description#> */
void *fun(void *param){
    NSLog(@"%@",[NSThread currentThread]);
    return NULL;
}

@end

打印结果:

2016-03-07 22:29:00.719 多线程之pthread[80573:1598184] {number = 2, name = (null)}

你可能感兴趣的:(多线程,ios,pthread)