线程的创建、等待、退出

多线程开发在Linux平台上已经有成熟的pthread库支持,所以使用pthread库在编译时要加上-pthread。其设计的多线程开发的基本概念主要包含3点:线程、互斥锁、条件。其中线程操作又分线程的创建、退出、等待三种。互斥锁包含4种操作,分别是创建、销毁、加锁和解锁。条件操作有5种,分别是"创建、销毁、触发、广播和等待。

更详细的讲解可以看以下的连接:https://www.cnblogs.com/xiehongfeng100/p/4620852.html

线程创建

函数原型

int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);

参数

pthread_t *restrict tidp:新线程的ID。需要注意的是使用pthread_t定义的变量是unsigned int型,传参的时候要进行类型转换。

pthread_attr_t *restrict attr:线程的属性。可以暂时把它设置为NULL,创建默认属性的线程。

void *(*start_rtn)(void *arg):线程创建需要执行的任务函数。新创建的线程从start_rtn函数的地址开始运行。该函数只有一个无类型指针arg。如果需要start_rtn函数传递的参数不止一个,那么需要将这些参数放入一个结构体中,然后把这个结构体的地址作为arg参数传入。

返回值

创建函数成功返回0,失败返回错误编号。

示例

cpp 复制代码
#include <stdio.h>
#include <pthread.h>
void *func1(void *arg)
{
        printf("t1:%ld thread is creart\n",(unsigned long)pthread_self());
        printf("t1:param is %d\n",*((int *)arg));
}
int main()
{
        int param=100;
        int ret;
        pthread_t t1;
        ret=pthread_create(&t1, NULL,func1, (void *)&param);
        if(ret == 0)
        {
                printf("main:create t1 successed\n");
        }


        printf("main:%ld\n",(unsigned long)pthread_self());
        while(1);

        return 0;
}
        

线程等待

调用这个函数的线程将一直阻塞,直到指定的此案成调用pthread_exit,从启动例程中返回或被取消。如果例程只是从他的启动例程返回i,rval_ptr将包含返回码。如果线程被取消,由rval_ptr指定的内存单元就置为PTHREAD_CANCELED。

可以通过掉用pthread_join自动把线程职位分离状态,这样资源就可以回复。如果线程已经处于分离状态,pthread_join调用就会失败,返回EINVAL。

如果对线程的返回值不感兴趣,可以把rval_ptr置为NULL。在这种情况下,调用pthread_join函数将等待指定的线程终止,但并不获得线程的终止状态。

函数原型

int pthread_join(pthread_t thread, rval_ptr);

参数

pthread_t thread:线程ID

void **rval_ptr:线程收回或被取消的状态等信息

返回值

若成功返回0,否则返回错误编号

线程退出

单个线程可以通过以下三种方式退出,在不终止整个进程的情况下停止它的控制流

  1. 线程只是从启动例程中返回,返回值是
  2. 线程可以被统一进程中的其他线程取消
  3. 线程调用pthread_exit;

函数原型

int pthread_exit(void *rval_ptr);

参数

void *rval_ptr:是一个无类型指针,与传给启动例程的单个参数类似。进程中的其他线程可以通过pthread_join函数访问到这个指针。

返回值

线程的退出码

示例

cpp 复制代码
#include <stdio.h>
#include <pthread.h>
void *func1(void *arg)
{
        static int ret=10;
        printf("t1:%ld thread is creart\n",(unsigned long)pthread_self());
        printf("t1:param is %d\n",*((int *)arg));
        pthread_exit((void *)&ret);

}
int main()
{
        int param=100;
        int *pret;
        int ret;
        pthread_t t1;

        ret=pthread_create(&t1, NULL,func1, (void *)&param);
        if(ret == 0)
        {
                printf("main:create t1 successed\n");
        }


        printf("main:%ld\n",(unsigned long)pthread_self());

        pthread_join(t1,(void **)&pret);
        printf("main: ti quit:%d\n",*pret);
        return 0;
}

除了可以传递整型变量,也可以传字符串等其他类型变量,但是需要注意的是,使用pthread_exit函数返回的变量要用static进行声明,不然传递的数据会出错。

cpp 复制代码
#include <stdio.h>
#include <pthread.h>
void *func1(void *arg)
{
        static int ret=10;
        static char *p="t1 is run out";
        printf("t1:%ld thread is creart\n",(unsigned long)pthread_self());
        printf("t1:param is %d\n",*((int *)arg));
        pthread_exit((void *)p);

}
int main()
{
        int param=100;
        char *pret=NULL;
        int ret;
        pthread_t t1;

        ret=pthread_create(&t1, NULL,func1, (void *)&param);
        if(ret == 0)
        {
                printf("main:create t1 successed\n");
        }


        printf("main:%ld\n",(unsigned long)pthread_self());

        pthread_join(t1,(void **)&pret);
        printf("main: ti quit:%s\n",pret);
        return 0;
}

线程ID获取

  对于线程ID比较,为了可移植操作,我们不能简单地把线程ID当作整数来处理,因为不同系统对线程ID的定义可能不一样。

函数原型

pthread_t pthread_self(void);

返回值

调用线程的ID

线程ID比较

函数原型

int pthread_equal(pthread_t tid1, pthread_t tid2);

返回值

若相等则返回非0值,否则返回0

复制代码
复制代码
相关推荐
毒爪的小新4 小时前
Linux 环境极速部署 vLLM:从零搭建生产级大模型推理服务
linux·人工智能·ai·语言模型·vllm
鹤落晴春5 小时前
RH124问答3:从命令行管理文件
linux·运维·服务器
凡人叶枫5 小时前
Effective C++ 条款30:透彻了解 inlining 的里里外外
linux·开发语言·c++·嵌入式开发·effective c++
noipp5 小时前
推荐题目:洛谷 P10907 [蓝桥杯 2024 国 B] 蚂蚁开会
c语言·c++·算法·编程·洛谷
Net_Walke6 小时前
【Linux系统】静态链接库与动态链接库
linux·嵌入式硬件
syc78901236 小时前
中文语境下AI编码工具实战对比:从迭代体验看日常开发选择
linux·人工智能·ubuntu
凡人叶枫6 小时前
Effective C++ 条款22:将成员变量声明为 private
linux·开发语言·c++
努力小周7 小时前
STM32智能安防系统
c语言·stm32·单片机·嵌入式硬件·物联网·计算机网络·pcb工艺
vsropy8 小时前
Ubuntu网络图标消失问题/有网络问号
linux·运维·ubuntu
coderwu8 小时前
Ubuntu 24.04 终端输入 openclaw config 提示未找到命令解决办法
linux·运维·ubuntu