多线程编程——pthread

1 创建线程

1.1 原型

#include <pthread.h>

int pthread_create(pthread_t *tid, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

1.2 功能

创建一个线程
新线程从start_routine开始执行
新线程的ID保存在tid指向的位置

1.3 参数

参数功能
tid该参数是一个指针, 新线程的ID保存在tid指向的位置
attr线程属性。如果为空,则使用缺省的属性值
start_routine该参数是一个函数指针, 新线程从start_routine开始执行

1.4 返回值

如果成功,返回0
如果失败,返回非0

1.5 例子

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

// int pthread_create(pthread_t *tid, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
void *compute(void *arg)
{
    char i;
    for (i = 'a'; i < 'd'; i++) {
        printf("worker: %c\n", i);
        sleep(1);
    }
    return NULL;
}

int main()
{ 
    pthread_t worker_tid;
    pthread_create(&worker_tid, NULL, &compute, NULL);

    char i;
    for (i = 'A'; i < 'D'; i++) { 
        printf("master: %c\n", i);
        sleep(1);
    }
    return 0;
}

2 参数类型

参数类型
可以向线程入口函数传递任意类型的参数
整型变量
long ivalue = 123;
void *arg = (void *) ivalue;
pthread_create(&tid, NULL, start_routine, arg);

字符串变量
char *svalue = "string";
void *arg = (void *) svalue;
pthread_create(&tid, NULL, start_routine, arg);

结构体变量,只能传递结构体的地址
struct person {
    char *name; 
    int age;
} p;
void *arg = (void *) &p;
pthread_create(&tid, NULL, start_routine, arg);

2.1 传int类型

  1. 进行相应的类型转换

int类型转换到void*存在size不一样的问题

pthread_create(&worker_tid, NULL, &compute2, (void *)100);

在这里插入图片描述
解决:用lnog int 类型
pthread_create(&worker_tid, NULL, &compute2, (void *)(long int)100);

void *compute2(void *arg)
{
        long num = (long int)arg;
        int i;
        for (i = 0; i < 3; i ++)
        {
                printf("i=%d, num=%ld\n", i, num);
                sleep(1);
        }
        return NULL;
}


int main()
{
        long ivalue = 100;
        pthread_t worker_tid;
//      pthread_create(&worker_tid, NULL, &compute, "worker");
        pthread_create(&worker_tid, NULL, &compute2, (void *)ivalue);
        compute2((void*)(long int)(200));
        return 0;
}

将ivalue定义为long 类型,注意: long类型强制类型转换需要用(long int)

  1. 或者传指针
void *compute3(void *arg)
{
        int *num = (int *)arg;
        int i;
        for (i = 0; i < 3; i ++)
        {
                printf("i=%d, num=%d\n", i, *num);
                sleep(1);
        }
        return NULL;
}

int main()
{
        long ivalue = 100;
        pthread_t worker_tid;
//      pthread_create(&worker_tid, NULL, &compute, "worker");
//      pthread_create(&worker_tid, NULL, &compute2, (void *)ivalue);
        pthread_create(&worker_tid, NULL, &compute3, (void *)&ivalue);
        compute2((void*)(long int)(200));
        return 0;

3 等待线程

3.1等待线程

3.1.1原型
#include <pthread.h>

int pthread_join(pthread_t tid, void **result);
3.1.2 功能

等待线程结束

3.1.3 参数

tid 目标线程的ID
result 用于存放线程的计算结果

3.1.4 返回值

如果成功,返回0
如果失败,返回非0

3.2 线程返回值

线程入口函数返回类型为void **类型的结果
void *start_routine(void *arg)
{
    void *result;  //返回多个可用结构体类型
    result = malloc(……);
    ...
    return result;
}

等待线程函数pthread_join获取线程的返回结果
void *result;
pthread_join(tid, &result);

https://blog.csdn.net/weixin_43587255/article/details/105741997
线程返回时有如下注意点:线程函数中指针需要申请分配空间,如果不分配,其将指向一个局部变量,函数调用完释放,造成指针悬挂,指向不确定的地方

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值