zoukankan      html  css  js  c++  java
  • 多线程02---pThread简单介绍

    1.简单介绍

    pthread 是属于 POSIX 多线程开发框架。

    它是c语言提供的一个跨平台的多线程解决方式。因为其在iOS编程中,操作比較麻烦。一般不用,这里介绍只作为了解。

    2.pthread的使用

    通过下面函数创建pthread,在C语言中类型的结尾通常 _t/Ref,并且不须要使用 *:

    int pthread_create(pthread_t * __restrict, const pthread_attr_t * __restrict,
            void *(*)(void *), void * __restrict);

    參数:

     1. pthread_t * __restrict 线程代号的地址
     2. onst pthread_attr_t * __restrict 线程的属性
     3. 调用函数的指针
        - void *(*)(void *)
        - 返回值 (函数指针)(參数)
        - void * 和 OC 中的 id 是等价的
     4. void * __restrict :传递给该函数的參数
    

    返回值:

    • 假设是0,表示正确
    • 假设是非0,表示错误码

    3.代码实战:

    #import "ViewController.h"
    #import <pthread.h>
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSString *str = @"MR";
    
        pthread_t thread;
        pthread_create(&thread, NULL, &demo, (__bridge void *)(str));
    }
    
    void* demo(void* params)
    {
        NSString *str = (__bridge NSString *)(params);
        for (int i = 0 ; i < 100; i++) {
    
            NSLog(@"%@",str);
        }
        return NULL;
    }
    @end

    __bridge(桥接)

    1. 在 ARC 开发中。假设设计到和 C 语言中同样的数据类型进行转换时。须要使用 __bridge “桥接”
    2. 在 OC 中,假设是 ARC 开发,编译器会在编译的时候,自己主动依据代码结构,加入 retain, release, autorelease
    3. ARC 只负责 OC 部分的代码。不负责 C 的代码。假设 C 语言的框架出现 retain/create/copy 字样的函数。都须要release

    执行结果
    这里写图片描写叙述

  • 相关阅读:
    高阶函数
    如何理解data URL?
    JS对象的创建方式
    数组中的map方法
    奇怪的 Javascript
    什么场景不适合箭头函数
    协方差矩阵学习[转载]
    PAT 1140 Look-and-say Sequence [比较]
    PAT 1089 Insert or Merge[难]
    PAT 1096 Consecutive Factors[难]
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7061104.html
Copyright © 2011-2022 走看看