线程的创建、等待、退出

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

复制代码
复制代码
相关推荐
三8449 小时前
文件查找/文件压缩/解压缩
linux·运维·服务器
小猪写代码9 小时前
Linux 管道(Pipeline)作业
linux·运维·服务器
会周易的程序员11 小时前
microLog 的本地日志读取接口 log_reader — 本地日志文件读取工具开发指南
linux·物联网·架构·嵌入式·日志·iot·aiot
灯厂码农11 小时前
C语言动态内存分配完全指南(malloc、calloc、realloc、free)
java·c语言·算法
yoothey11 小时前
报废审批流规则引擎设计——责任链模式完整实现
linux·开发语言·bash
2501_9259633811 小时前
外设的常见问题
linux
wuyk55511 小时前
24. C 语言模块化:不是拆几个.c 文件那么简单
c语言·开发语言·stm32·单片机
l1t12 小时前
在linux和windows中解决duckdb 1.6dev版本输出执行计划报错问题
linux·运维·数据库·windows·duckdb
qq_2415856112 小时前
可用在中断中浮点数打印类似printf
c语言
柳鲲鹏12 小时前
LINUX高通平台交叉编译地图软件GDAL
linux