0.导入头文件
#import<pthread.h>
1.创建线程对象
pthread_t threadA;
2.创建线程
- pthread_create(<#pthread_t _Nullable *restrict _Nonnull#>, <#const pthread_attr_t restrict _Nullable#>, <#void * _Nullable ( _Nonnull)(void * _Nullable)#>, <#void *restrict _Nullable#>)
第一个参数:线程对象 传递地址
第二个参数:线程的属性 NULL
第三个参数:指向函数的指针
第四个参数:函数需要接收的参数 - 如何写第三个参数:
1.双击第三个参数
2.将第一个星后面一直到最后一个括号,用一个名字代替(函数名)
3.第二个括号的星后面用一个名字代替(参数名)
pthread_create(&threadA , NULL , task , NULL);
void *task(void *param){
NSLog(@"%@" ,[NSThread currentThread]);
return NULL;
}
如何创建多个线程
- 在次创建一个线程对象,在此调用线程的方法,在里面的第一个函数写上第二个线程对象。可以让他们共同调用一个函数(方法)