欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

pThread多线程demo

程序员文章站 2024-01-07 09:46:34
#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;
}

 

上一篇:

下一篇: