pthread_create不常用了解即可。
pthread_create是类Unix操作系统(Unix、Linux、Mac OS X等)的创建线程的函数,pthread提供了一套线程API,若线程创建成功,则返回0。若线程创建失败,则返回出错编号。
1、使用前需要导入系统的头文件
#import <pthread/pthread.h>
2、pthread_create创建线程
参数一:线程标识符
参数二:线程属性
参数三:线程回调方法
参数四:传参数
pthread_create(<#pthread_t _Nullable *restrict _Nonnull#>, <#const pthread_attr_t *restrict _Nullable#>, <#void * _Nullable (* _Nonnull)(void * _Nullable)#>, <#void *restrict _Nullable#>)
创建方法一:
// 传参数
char *name = "zw";
pthread_t thread;
pthread_create(&thread, NULL, demo, name);
void *demo(void *param){
NSLog(@"hello %s",param);
return NULL;
}
创建方法二:
// 无参数
pthread_t thread;
pthread_create(&thread, NULL, (void*)thread_1, NULL);
void thread_1(void)
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread_1.\n");
pthread_exit(0);
}