线程的创建、等待、退出

多线程开发在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

复制代码
复制代码
相关推荐
神仙别闹12 分钟前
基于C语言 HTTP 服务器客户端的实验
服务器·c语言·http
小狗爱吃黄桃罐头23 分钟前
正点原子【第四期】Linux之驱动开发学习笔记-10.1 Linux 内核定时器实验
linux·驱动开发·学习
Kang强1 小时前
tcpdump 抓到 icmp 包,但是抓不到 tcp 包??
linux
demodashi6661 小时前
Linux下ag搜索命令详解
linux·运维·windows
GilgameshJSS1 小时前
STM32H743-ARM例程40-U_DISK_IAP
c语言·arm开发·stm32·单片机·嵌入式硬件
無识2 小时前
Linux-第四章web服务
linux·运维·服务器
No0d1es2 小时前
电子学会青少年软件编程(C/C++)1级等级考试真题试卷(2025年9月)
java·c语言·c++·青少年编程·电子学会·真题·一级
一叶知秋yyds2 小时前
Centos 安装 Docker教程
linux·docker·centos
fie88892 小时前
在CentOS 7上集成cJSON库的方法
linux·运维·centos
带土12 小时前
5. Unix/Linux 系统常用类型别名清单
linux·unix