zoukankan      html  css  js  c++  java
  • pThread多线程demo

    #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;
    }
  • 相关阅读:
    Outlook 邮件助手
    飞花令
    青蛙跳台阶
    如何提问,找到去说谎国的路
    如何计时一个小时十五分钟
    旋转数组的最小元素
    谁养鱼?
    小龙赚了多少?
    下一行是什么?
    5 = ?
  • 原文地址:https://www.cnblogs.com/xuzb/p/9138726.html
Copyright © 2011-2022 走看看