iOS 线程之pthread

本文介绍了类Unix操作系统中pthread_create函数的使用方法,包括如何创建带参数和无参数的线程。通过示例展示了pthread_create的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pthread_create不常用了解即可。

pthread_create是类Unix操作系统(Unix、Linux、Mac OS X等)的创建线程的函数,pthread提供了一套线程API,若线程创建成功,则返回0。若线程创建失败,则返回出错编号。

1、使用前需要导入系统的头文件

#import <pthread/pthread.h>

2、pthread_create创建线程

参数一:线程标识符
参数二:线程属性
参数三:线程回调方法
参数四:传参数

pthread_create(<#pthread_t  _Nullable *restrict _Nonnull#>, <#const pthread_attr_t *restrict _Nullable#>, <#void * _Nullable (* _Nonnull)(void * _Nullable)#>, <#void *restrict _Nullable#>)

创建方法一:

    // 传参数
    char *name = "zw";
    pthread_t thread;
    pthread_create(&thread, NULL, demo, name);

void *demo(void *param){
    NSLog(@"hello %s",param);
    return NULL;
}

创建方法二:

    // 无参数
    pthread_t thread;
    pthread_create(&thread, NULL, (void*)thread_1,     NULL);


void thread_1(void)
{
    int i;
    for(i=0;i<3;i++)
        printf("This is a pthread_1.\n");
    pthread_exit(0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值