pThread多线程demo
程序员文章站
2022-03-22 14:53:52
#import "ViewController.h" #import @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButto... ......
#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; }
上一篇: 课时30.无序列表(掌握)