#import "ViewController.h"
#import <pthread.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 30);
[btn setTitle:@"pThread" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(clickPThread) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)clickPThread {
NSLog(@"我在主线程中执行!!!");
pthread_t pthread;
pthread_create(&pthread, NULL, run, NULL);
}
// C语言写法
void *run(void *data) {
NSLog(@"我在子线程中执行!!!");
for (int i = 1; i < 10; i++) {
NSLog(@"%d", i);
sleep(1);
}
return NULL;
}