linux系统编程 线程 p1

1.线程的概念

线程就是一个正在运行的函数。
posix线程是一套标准,而不是实现。
openmp线程。
线程标识:pthread_t (linux环境下是整形数)
就像每个进程有一个进程ID一样,每个线程也有一个线程ID。进程ID在整个系统中是唯一的,但线程ID不同,线程ID只有在它所属的进程上下文中才有意义。
进程ID是用pid_t数据类型来表示的,是一个非负整数。线程ID是用pthread_t 数据类型来表示的,实现的时候可以用一个结构来代表pthread_t数据类型,所以可移植的操作系统实现不能把它作为整数处理。

处理器也是以线程为调度单位。
线程实际是以进程标识来描述的。

pthread_equal() 比较线程标识;
pthread_self(void) 返回当前线程标识。

2.线程的创建/终止/取消,栈的清理

2.1线程创建

pthread_create();

NAME
       pthread_create - create a new thread

LIBRARY
       POSIX threads library (libpthread, -lpthread)

SYNOPSIS
       #include <pthread.h>

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

传入的参数可以做成结构体,传给arg。

程序实例

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <errno.h>
#include <cstring>

using namespace std;

static void* func(void* p) {
    cout << "thread is working" << endl;
    return 0;
}

int main() {
    cout << "begin" <<endl;
    pthread_t tid;
    int err;
    err = pthread_create(&tid, NULL, func, NULL);
    if (err) {
        cerr << "thread create:" << strerror(err) << endl;
        exit(0);
    }

    cout << "end" <<endl;
    exit(0);
}

[root@iZ2zeirmyddvdjh8g2zyxzZ pthread]# ./create1 
begin
end

没有出现thread is working
这种现象是因为:线程的调度取决于调度器的调度策略。

2.2线程终止

3种方式:
1)线程从启动例程中返回,返回值就是线程的退出码;
2)线程可以被同一进程中的其他线程取消
3)线程调用pthread_exit()函数。

pthread_exit()

NAME
       pthread_exit - terminate calling thread

LIBRARY
       POSIX threads library (libpthread, -lpthread)

SYNOPSIS
       #include <pthread.h>

       [[noreturn]] void pthread_exit(void *retval);

pthread_join() ----> wait相当于收尸

NAME
       pthread_join - join with a terminated thread

LIBRARY
       POSIX threads library (libpthread, -lpthread)

SYNOPSIS
       #include <pthread.h>

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

程序实例,补全刚才程序

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <errno.h>
#include <cstring>

using namespace std;

static void* func(void* p) {
    cout << "thread is working" << endl;
    pthread_exit(NULL);
    return 0;
}

int main() {
    cout << "begin" <<endl;
    pthread_t tid;
    int err;
    err = pthread_create(&tid, NULL, func, NULL);
    if (err) {
        cerr << "thread create:" << strerror(err) << endl;
        exit(0);
    }

    //等线程运行完等待收尸
    pthread_join(tid, NULL);
    cout << "endl" <<endl;
    exit(0);
}
[root@iZ2zeirmyddvdjh8g2zyxzZ pthread]# ./create1 
begin
thread is working
end

2.3 栈的清理

线程可以安排它退出时需要调用的函数,这与进程在退出时可以用atexit函数安排退出是类似的。这样的函数称为线程清理处理程序(thread cleanup handler)。一个线程可以建立多个清理处理程序。处理程序记录在栈中,也就是说,它们的执行顺序与它们注册时相反

NAME
       pthread_cleanup_push, pthread_cleanup_pop - push and pop thread cancelation clean-up handlers

LIBRARY
       POSIX threads library (libpthread, -lpthread)

SYNOPSIS
       #include <pthread.h>

       void pthread_cleanup_push(void (*routine)(void *), void *arg);
       void pthread_cleanup_pop(int execute);

钩子函数代码实例

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <errno.h>
#include <cstring>

using namespace std;

static void cleanup_function(void* arg) {
    cout << (const char*)arg << endl;
}

static void* func(void* p) {
    cout << "thread is working" << endl;
    string s1 = "haha1";
    string s2 = "haha2";

    // 使用 std::string 的 c_str() 方法来获取 const char* 类型的指针
    pthread_cleanup_push(cleanup_function, (void*)s1.c_str());
    pthread_cleanup_push(cleanup_function, (void*)s2.c_str());

    pthread_exit(NULL);

    //必须成对出现
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
    return 0;
}

int main() {
    cout << "begin" <<endl;
    pthread_t tid;
    int err;
    err = pthread_create(&tid, NULL, func, NULL);
    if (err) {
        cerr << "thread create:" << strerror(err) << endl;
        exit(0);
    }

    pthread_join(tid, NULL);
    cout << "end" <<endl;
    exit(0);
}
[root@iZ2zeirmyddvdjh8g2zyxzZ cleanup]# ./create1 
begin
thread is working
haha2
haha1
end

相关推荐

  1. linux系统编程 线 p1

    2024-04-03 06:06:03       34 阅读
  2. Linux编程-线1

    2024-04-03 06:06:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-03 06:06:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-03 06:06:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-03 06:06:03       82 阅读
  4. Python语言-面向对象

    2024-04-03 06:06:03       91 阅读

热门阅读

  1. 匈牙利算法的实现

    2024-04-03 06:06:03       33 阅读
  2. matlab学习(一)(3.26-4.1)

    2024-04-03 06:06:03       38 阅读
  3. C++类 单例模式

    2024-04-03 06:06:03       35 阅读
  4. Vue3:Pinia中的getters

    2024-04-03 06:06:03       39 阅读
  5. 前端开发语言有哪些

    2024-04-03 06:06:03       29 阅读
  6. pyenv+anaconda+python+opencv+tensorflow-gpu

    2024-04-03 06:06:03       32 阅读
  7. Vue组件中的v-model(深入解析)

    2024-04-03 06:06:03       37 阅读
  8. day 1 将go基础知识复习一下

    2024-04-03 06:06:03       30 阅读
  9. Ubuntu 23.04 安装es

    2024-04-03 06:06:03       41 阅读
  10. Node.js常用的命令

    2024-04-03 06:06:03       29 阅读
  11. kafka 常用非基础的核心设置项

    2024-04-03 06:06:03       33 阅读