当前位置: 首页 > article >正文

操作系统(5) (POSIX--Linux线程编程pthread_t/create/join/exit)

目录

1.pthread_create()

 2. pthread_join

3. pthread_exit()

4.pthread_t

5. 代码应用

解释:


1.pthread_create()

  • 作用:用于创建一个新的线程。
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
  • pthread_t *thread:存储线程的 ID。
  • const pthread_attr_t *attr:线程的属性。如果设置为 NULL,则使用默认属性。
  • void *(*start_routine)(void *):线程将要执行的函数。
  • void *arg:传递给线程函数的参数。

 2. pthread_join

作用:等待指定的线程完成执行,类似于进程中的 wait(),主线程会等待指定的子线程运行完毕再继续执行。

int pthread_join(pthread_t thread, void **retval);
  • pthread_t thread:需要等待的线程 ID。
  • void **retval:用于获取线程的返回值。如果不需要获取,可以传 NULL

3. pthread_exit()

作用:用于终止调用它的线程,可以提供一个返回值供其他线程通过 pthread_join() 获取。

void pthread_exit(void *retval);
  • void *retval:线程的返回值,供 pthread_join() 获取。 

4.pthread_t

  • 作用:这是一个数据类型,用于存储线程的 ID。类似于进程中的 pid_t,用于标识和管理不同的线程。
pthread_t thread_id;
pthread_create(&thread_id, NULL, &thread_function, (void *) &argument);
pthread_join(thread_id, NULL);
  • pthread_t thread_id;
  • pthread_create(&thread_id, NULL, &thread_function, (void *) &argument);
  • pthread_join(thread_id, NULL);
     
  • pthread_create() 用于创建线程。
  • pthread_join() 用于等待线程结束。
  • pthread_exit() 用于线程退出。
  • pthread_t 是线程的标识符类型,用于存储线程 ID。

5. 代码应用

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

// 线程函数,接收一个参数并打印该参数的值
void* print_message_function(void* arg) {
    char* message = (char*) arg;
    printf("%s\n", message);
    pthread_exit(NULL);  // 线程退出
}

int main() {
    pthread_t thread1, thread2;  // 定义两个线程 ID
    const char* message1 = "Thread 1: Hello, World!";
    const char* message2 = "Thread 2: Welcome to threading!";

    // 创建第一个线程,执行 print_message_function,传递 message1
    if (pthread_create(&thread1, NULL, print_message_function, (void*) message1) != 0) {
        printf("Error: Unable to create thread 1\n");
        exit(1);
    }

    // 创建第二个线程,执行 print_message_function,传递 message2
    if (pthread_create(&thread2, NULL, print_message_function, (void*) message2) != 0) {
        printf("Error: Unable to create thread 2\n");
        exit(1);
    }

    // 使用 pthread_join() 等待两个线程完成
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    // 主线程执行完毕
    printf("All threads completed.\n");

    return 0;
}

解释:

  1. 创建线程

    • pthread_create(&thread1, NULL, print_message_function, (void*) message1);
      • 这是创建线程的函数。
      • thread1 是线程的 ID。
      • NULL 代表默认线程属性。
      • print_message_function 是线程执行的函数。
      • (void*) message1 是传递给线程函数的参数。
  2. 线程函数 print_message_function

    • 线程函数接受一个 void* 类型的参数。在这里我们将其转换为 char*,并打印它。
    • pthread_exit(NULL); 用于退出线程。
  3. 等待线程完成

    • pthread_join(thread1, NULL);pthread_join(thread2, NULL); 用于等待线程 thread1thread2 完成。
    • NULL 表示不获取线程的返回值。
  4. 输出

程序将输出两个线程的消息,依次打印出:

Thread 1: Hello, World!
Thread 2: Welcome to threading!

然后,主线程会等待两个线程结束,最后打印 All threads completed.

编译和运行:与线程有关的要使用-pthread编译

gcc -o thread_example thread_example.c -pthread
./thread_example


http://www.kler.cn/news/356202.html

相关文章:

  • 【ARM 嵌入式 编译系列 2.9 -- GCC 编译如何避免赋值判断 if ( x = 0)】
  • 无人机之融合集群技术篇
  • JAVA基础-API-Arrays工具类
  • Gee引擎架设教程:Gee引擎人形怪物设置,MonUseItems配置文件讲解
  • 【安当产品应用案例100集】021- 针对电网接入设备的控制指令安全解决方案
  • 红队攻防之隐匿真实IP
  • 【VUE】为什么要避免v-for和v-if在一起使用
  • 【Unity新闻】Unity 6 正式版发布
  • 大模型带来新安全机遇
  • 【idea技巧篇】idea的类注释和方法注释模版自定义设置
  • 如果有100万条消息堆积在MQ怎么解决
  • 2024 年 9 月区块链游戏研报:行业回暖,Telegram 游戏引发热潮
  • [工程构建] 使用 pkg-config 的 cmake 模板
  • laravel-admin后台子账号菜单配置详解
  • ant-design-vue 时间选择器 a-date-picker 单组件设置国际化失效问题解决
  • 网数中心举办CISAW安全软件教师培训 助力国家网络安全战略
  • 众数信科荣登“2024 CHINA AIGC 100”榜单
  • 如何修改网络ip地址:一步步指南‌
  • 【C语言】sizeof和strlen的区别
  • 【C语言】指针与函数:传值与传址