pthread使用(多线程002)

由于pthread主要是c语言接口, 对于不同平台可移植性比较好, 对于大公司来说使用较多, 但是线程生命周期需要程序猿自己管理,所以对于可移植性要求不高的地方几乎很少有人使用, 这里只做一些简单的介绍和示例.  

1. 导入头文件

#import <pthread.h>

2. 使用创建函数

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

3. 参数解释

 pthread_t:线程的编号,地址

 pthread_attr_t:线程的属性

 void *(*)(void *):指定该线程要运行的方法(函数指针)

 void *:运行指定函数的参数

void * 指向任意类型的指针

函数指针前加上*号或&号或直接写函数名字都表示函数的首地址

4.返回值

0 :创建线程成功

非0:创建线程失败

5.Example

int result = pthread_create(&pid, NULL, &demo, (__bridge void *)str);


    if (result == 0) {
        //
        NSLog(@"成功");
    }else{
        NSLog(@"失败");
    }

void * demo(void *param)
{
    NSString *str = (__bridge NSString *)(param);

    NSLog(@"%@",str);

//    for (int i = 0; i < 10000; i++) {
//        NSLog(@"abc %@",[NSThread currentThread]);
//    }
    return NULL;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值