线程的创建、等待、退出

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

复制代码
复制代码
相关推荐
TC13981 小时前
服务器Ubuntu22.04 server 安装gnome GUI+VNC远程配置
linux·服务器·远程工作
Hacker_Oldv2 小时前
网络安全linux命令
linux·网络·web安全
于慨3 小时前
计算机考研C语言
c语言·开发语言·数据结构
路由侠内网穿透4 小时前
本地部署资源聚合搜索神器 Jackett 并实现外部访问
linux·运维·服务器·网络协议·tcp/ip
djykkkkkk5 小时前
ubuntu 和 RV1126 交叉编译Mosqutiio-1.6.9
linux·运维·ubuntu
蜕变的土豆6 小时前
二、重学C++—C语言核心
c语言·c++
好多知识都想学6 小时前
第二章Linux 命令概述
linux·运维·服务器
熊峰峰6 小时前
Linux第0节:Linux环境的搭建
linux·运维·服务器
鸭梨山大。6 小时前
linux命令-iptables与firewalld 命令详解
linux·运维·网络
半夏知半秋7 小时前
linux下的网络抓包(tcpdump)介绍
linux·运维·服务器·网络·笔记·学习·tcpdump