掉入库
#import <pthread.h>
pthread_t thread
static id object;
- (void)viewDidLoad {
[super viewDidLoad];
object = self;
// 2. 开启线程: 执行任务
pthread_create(&thread, NULL, run, NULL);
// 3. 设置子线程的状态设置为 detached,该线程运行结束后会自动释放所有资源
pthread_detach(thread);
void *time_Out(void *param) // 新线程调用方法,里边为需要执行的任务
{
int i = 1;
while (i) {
printf("this is the 1st pthread,created by zieckey.\n");
//睡 50毫秒
usleep(50*1000);
i += 50;
printf("i===============.%d\n",i);
if (i > 5000) {
printf("i=========关闭线程======.%d\n",i);
//关闭线程
pthread_cancel(thread);
return NULL;
}
}
return NULL;
}