长生栈 长生栈
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Living Team

编程技术分享
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 文件操作

  • Linux进程和线程

    • 进程创建
    • 在进程中启动程序
    • 等待进程结束
    • 线程简介
    • 多线程编程举例
      • 实现线程的函数
        • 创建新线程
        • 退出线程
        • 合并线程
      • 线程编程举例
      • 进一步讨论pthread_join()
    • 线程同步
    • 线程属性
  • Linux信号

  • 进程间通信

  • Socket

  • C语言
  • Linux进程和线程
DC Wang
2023-02-17
目录

多线程编程举例

# 多线程编程举例

# 实现线程的函数

  • 创建新线程:pthread_create

  • 终止线程:pthread_exit

  • 合并线程:pthread_join

# 创建新线程

#include <pthread.h>
int pthread_create(
    pthread_t* thread,    //指向线程标识符的指针
    pthread_attr* attr,   //设置线程属性
    void *(*func)(void*), //新线程将启动的函数的地址
    void* arg             //新线程将启动的函数的参数
)
1
2
3
4
5
6
7
  • 函数执行成功返回0,失败返回错误码。

  • 执行成功后,新线程将从设定的函数处开始执行,原线程则继续执行。

# 退出线程

#include <pthread.h>
void pthread_exit(void* ret)
1
2
  • 终止调用此函数的线程,并返回一个指向某对象的指针(ret)。注意,不能返回指向局部变量的指针。

# 合并线程

#include <pthread.h>
int pthread_join(pthread_t* thread, void **ret);
1
2
  • 等价于进程中用于收集子进程的wait()函数,thread是要等待的线程(即通过pthread_create函数返回的线程标识符),ret是一个二级指针,它指向一个指针,后者指向线程的返回值。
  • 函数执行成功时返回0,失败时返回错误码。

# 线程编程举例

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

void *thread_function(void *arg);

char message[] = "Hello World";

int main() {
    int res;
    pthread_t a_thread;
    void *thread_result;
    res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
    if (res != 0) {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
    printf("Waiting for thread to finish...\n");
    res = pthread_join(a_thread, &thread_result);
    if (res != 0) {
        perror("Thread join failed");
        exit(EXIT_FAILURE);
    }
    printf("Thread joined, it returned %s\n", (char *)thread_result);
    printf("Message is now %s\n", message);
    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
    printf("thread_function is running. Argument was %s\n", (char *)arg);
    sleep(3);
    strcpy(message, "Bye!");
    pthread_exit("Thank you for the CPU time");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

编译

$ gcc thread1.c -o thread1 -lpthread
1

输出

$ ./thread1
Waiting for thread to finish...
thread_function is running. Argument was Hello World
Thread joined, it returned Thank you for the CPU time
Message is now Bye!
1
2
3
4
5

# 进一步讨论pthread_join()

  • pthread_join的功能是等待某线程结束,并收集其返回值。

  • 初始线程结束后(从main函数返回),整个进程即结束,导致进程内所有线程全部终止。

  • 在主线程中调用pthread_exit,进程将等待所有线程结束后才终止。

编辑 (opens new window)
#C#Linux#线程
上次更新: 2023/02/18, 10:09:42
线程简介
线程同步

← 线程简介 线程同步→

最近更新
01
ESP32-网络摄像头方案
06-14
02
ESP32-PWM驱动SG90舵机
06-14
03
ESP32-实时操作系统freertos
06-14
更多文章>
Theme by Vdoing | Copyright © 2019-2025 DC Wang All right reserved | 辽公网安备 21021102001125号 | 吉ICP备20001966号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式