CentOS 使用线程库Pthread 库-CSDN博客

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

1、Pthread 库说明

pthread 库是Linux系统默认线程库。

在Linux 系统环境中编辑C/C++程序使用pthread 库需要添加对应的头文件并链接pthread库。

#include<pthread.h>

2、Pthread 库核心方法

pthread_create

函数定义

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

参数说明 

pthread_t 定义如下

typedef unsigned long int pthread_t;

thread 是一个指向线程标识符的指针线程调用后改值被设置为线程ID

attr 用来设置线程属性

start_routine 是线程函数的其实地址即线程函数体线程创建成功后thread 指向的内存单元从该地址开始运行

arg 是传递给线程函数体的参数

返回值

若线程创建成功则返回0失败则返回错误码并且 thread 内容是未定义的。

pthread_join

函数定义

int pthread_join(pthread_t thread, void **retval);

参数说明 

thread 是线程表示符

retval 用来获取线程的返回值一般是 pthread_join 方法传递出来的值

功能说明

这是一个线程阻塞函数调用该函数则等到线程结束才继续运行

pthread_exit

函数定义

void pthread_exit(void *retval);

参数说明 

retval 是线程的退出码传递给创建线程的地方

功能说明

一个线程的结束有两种途径

  • 线程函数体执行结束
  • 调用 pthread_exit 方法退出线程

pthread_self

函数定义:

pthread_t pthread_self();

功能说明

用来获取当前线程ID

pthraad_detach

函数定义

int pthread_detach (pthread_t __th)

功能说明:

分离线程

3、Pthread 库线程属性说明

线程属性 

设置线程不同属性有不同属性有不同的方法但是都需要先初始化属性数据结构初始化函数为

int pthread_attr_init(pthread_attr_t *__attr);

线程属性包括

  1. 作用域
  2. 栈大小
  3. 栈地址
  4. 优先级
  5. 分离状态
  6. 调度策略
  7. 调度参数

 分离状态

线程终止时系统将不再保留线程终止状态当不需要线程的终止状态时可以分离线程调用 pthread_detach 函数也可以通过设置线程的分离状态实现

int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state);
int pthread_attr_setdetachstate(pthread_attr_t* attr, int state);

state 的值可以是 PTHREAD_CREATE_DETACHED 和 PTHREAD_CREATE_JOINABLE分别表示主线程阻塞和子线程剥离

线程优先级 

新线程的优先级默认为0

int  pthread_attr_getschedparam(const pthread_attr_t *restrict attr, struct sched_param *restrict param) ;
int pthread_attr_setschedparam(pthread_attr *restrict attr, const struct sched_param* restrict param);

继承父优先级 

新线程不继承父线程的调度优先级

调度策略

线程使用 SCHED_OTHER 调度策略线程一旦开始运行直到被强占或者直到线程阻塞或者停止位置

int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy);
int pthread_attr_setschedparam(pthread_attr_t* attr, struct sched_param* param)

4、Pthread 示例代码

前提在/usr/local/source_code目录下新增thread_demo 目录并在此目录下新建init_thread.cpp文件内容如下

#include <pthread.h>
#include <iostream>

using namespace std;

void* printHello(void* args) {
    cout << "Hello World from Thread" << endl;
    return NULL;
}

int main() {
    pthread_t thread;

    if (pthread_create(&thread, NULL, &printHello, NULL)) {
        cout << "Error creating thread" << endl;
        return 1;
    }

    if (pthread_join(thread, NULL)) {
        cout << "Error joining thread" << endl;
        return 2;
    }

    return 0;
}

编译执行

g++ init_thread.cpp -o init_thread  -L/usr/local/lib/ -lpthread

控制台输出:

[root@localhost thread_demo]# g++ init_thread.cpp -o init_thread  -L/usr/local/lib/ -lpthread
[root@localhost thread_demo]# ll
总用量 16
-rwxr-xr-x. 1 root root 9136 10月 24 14:44 init_thread
-rw-r--r--. 1 root root  459 10月 24 14:30 init_thread.cpp
[root@localhost thread_demo]# ./init_thread
Hello World from Thread

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: centos