• pthread创建线程的简单演示


     
    使用pthread创建子线程的简单步骤
    1. 导入头文件 #import <pthread.h>
    2. 指定新线程标识符
    3. 使用pthread创建线程的函数
    4. 根据result = 0 与否判断子线程创建成功与否
        对创建子线程的函数的简单解析
    •      int pthread_create(pthread_t _Nullable * _Nonnull __restrict,
    •      const pthread_attr_t * _Nullable __restrict,
    •      void * _Nullable (* _Nonnull)(void * _Nullable),
    •      void * _Nullable __restrict);

         参数的意义分别为:

    •      pthread_t _Nullable * _Nonnull __restrict          传入到线程标识符的指针地址
    •      const pthread_attr_t * _Nullable __restrict        线程属性:传入指向线程属性的指针地址
    •      void * _Nullable (* _Nonnull)(void * _Nullable)    新线程要执行的函数(任务),传入函数地址即函数名
    •      void * _Nullable __restrict:                       传入到函数的参数
    •      返回值为整型                                         0表示创建线程成功 否则创建线程失败
    •      参数3的进一步解释

         void * _Nullable    (* _Nonnull)   (void * _Nullable)

     
         函数返回值类型            函数名          函数参数
     
    上代码
     
     
     1 #import "ViewController.h"
     2 #import <pthread.h>
     3 
     4 @interface ViewController ()
     5 @property (weak, nonatomic) IBOutlet UIButton *pthreadCreateThreadBtn;
     6 
     7 @end
     8 
     9 @implementation ViewController
    10 
    11 - (void)viewDidLoad {
    12     [super viewDidLoad];
    13     
    14 }
    15 #pragma mark - 创建线程的点击事件
    16 - (IBAction)pthreadCreateThreadBtnClick:(id)sender {
    17     
    18     [self pthreadDemo];
    19     [self pthreadDemo2];
    20     
    21 }
    22 
    23 
    24 #pragma mark - pthread 创建子线程的代码实现
    25 - (void)pthreadDemo{
    26     //参数1: 新线程的标识符
    27     pthread_t ID;
    28     
    29     //创建 指定标识符 指定参数为空 的线程
    30     int result = pthread_create(&ID, NULL, demo, NULL);
    31     if (result == 0) {
    32         NSLog(@"未传参数线程创建成功");
    33     }else{
    34         NSLog(@"未传参数线程创建失败");
    35     }
    36     
    37     
    38 }
    39 
    40 #pragma mark - pthread 创建子线程的代码实现
    41 - (void)pthreadDemo2{
    42 //    NSLog(@"pthreadDemo2 = %@",[NSThread currentThread]);
    43     //参数1: 新线程的标识符
    44     pthread_t id2;
    45     NSString *ocStr = @"hello iOS";
    46     
    47     //创建 指定标识符  指定参数 的线程
    48 //    Implicit conversion of Objective-C pointer type 'NSString *' to C pointer type 'void *' requires a bridged cast
    49     int result = pthread_create(&id2, NULL, demo2, (__bridge void *)(ocStr));
    50     if (result == 0) {
    51         NSLog(@"传了参数的线程创建成功");
    52     }else{
    53         NSLog(@"传了参数的线程创建失败");
    54     }
    55     
    56     
    57 }
    58 
    59 
    60 #pragma mark - 创建的无参数新线程执行的函数
    61 void *demo(void *param){
    62     NSLog(@"用于创建无参数线程当前线程%@",[NSThread currentThread]);
    63     return NULL;
    64 }
    65 
    66 
    67 #pragma mark - 创建的有参数新线程执行的函数
    68 void *demo2(void *param){
    69 //    Implicit conversion of Objective-C pointer type 'NSString *' to C pointer type 'void *' requires a bridged cast
    70     NSString *str = (__bridge NSString *)(param);
    71     
    72     NSLog(@"用于创建的有参数%@的线程-当前线程%@",str,[NSThread currentThread]);
    73     return NULL;
    74 }
    75 
    76 @end

         

        

    我会不定期分享 iOS 相关技术文章
  • 相关阅读:
    highcharts的表名
    highcharts的引用
    Factory 模式
    PHP中interface与 implements 关键字
    PHP中为位运算符(几乎很少用)
    &,引用复制@,忽略错误提示
    TP中手动加载类库
    TP框架自动加载优先级
    交换机和路由器
    Ucenter,Discuz
  • 原文地址:https://www.cnblogs.com/ITCoderW/p/6183038.html
Copyright © 2020-2023  润新知