深入了解pthread_create()

pthreads是一种POSIX线程库标准,可以在多个系统中使用。在C和C++中使用pthread库以及pthread_create函数来创建和控制线程。

一、pthread_create()介绍

pthread_create()函数用于创建一个线程。这个函数被大多数POSIX线程库实现支持。这个函数的原型如下:

    #include <pthread.h>

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

pthread_create()的参数:

  • thread: 指向pthread_t类型的指针,返回线程的真实标识符。
  • attr(Optional): 线程属性。
  • start_routine: 指向线程执行的函数。线程函数的类型为 void *(*start_routine) (void *arguments),其中arguments是传递给该线程函数的参数。
  • arg: 传递给线程函数的参数。

二、创建线程的方法

1. 传递函数指针

我们可以像下面这样定义一个线程函数:

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

    void* threadFunc1() {
        printf("Hello, this is threadFunc1\n");
        pthread_exit(NULL);
    }

    int main() {
        pthread_t thread1;
        printf("Before creating thread\n");
        pthread_create(&thread1, NULL, threadFunc1, NULL);
        printf("After creating thread\n");
        pthread_exit(NULL);
    }

运行结果:

    Before creating thread
    After creating thread
    Hello, this is threadFunc1

2. 传递一个对象提供线程函数访问

如下示例代码,我们可以定义一个结构,并将结构作为参数传递给线程函数。

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

    struct thread_data {
        int thread_id;
        char *message;
    };

    void* threadFunc2(void *args) {
        struct thread_data *data = (struct thread_data *) args;
        printf("Thread ID: %d\n", data -> thread_id);
        printf("Message: %s\n", data -> message);
        pthread_exit(NULL);
    }

    int main() {
        pthread_t thread2;
        struct thread_data data;
        data.thread_id = 123;
        data.message = "Hello, this is threadFunc2";
        printf("Before creating thread\n");
        pthread_create(&thread2, NULL, threadFunc2, (void *) &data);
        printf("After creating thread\n");
        pthread_exit(NULL);
    }

运行结果:

    Before creating thread
    After creating thread
    Thread ID: 123
    Message: Hello, this is threadFunc2

三、线程属性

可以使用pthread_attr_init()函数来初始化一个线程属性对象,然后使用这个线程属性对象来设置线程的属性。以下示例代码展示如何使用pthread_attr_setdetachstate()设置线程的分离属性。

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

    void* threadFunc3() {
        printf("Hello, this is threadFunc3\n");
        pthread_exit(NULL);
    }

    int main() {
        pthread_t thread3;
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        printf("Before creating thread\n");
        pthread_create(&thread3, &attr, threadFunc3, NULL);
        printf("After creating thread\n");
        pthread_attr_destroy(&attr);
        pthread_exit(NULL);
    }

运行结果:

    Before creating thread
    After creating thread
    Hello, this is threadFunc3

四、线程ID和join()

每个线程都有一个唯一标识符的线程ID,可以使用pthread_self()函数获取当前线程的ID。

可以使用pthread_join()来等待线程终止。和wait()函数类似,pthread_join()会阻塞调用者直到指定的线程终止。以下示例代码在创建线程后等待线程终止。

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

    void* threadFunc4() {
        printf("Hello, this is threadFunc4\n");
        pthread_exit(NULL);
    }

    int main() {
        pthread_t thread4;
        printf("Before creating thread\n");
        pthread_create(&thread4, NULL, threadFunc4, NULL);
        printf("After creating thread\n");
        pthread_join(thread4, NULL);
        pthread_exit(NULL);
    }

运行结果:

    Before creating thread
    After creating thread
    Hello, this is threadFunc4

五、总结

pthreads是一种POSIX线程库标准,可以在多个系统中使用。pthread_create()函数用于创建一个线程。可以传递函数指针或一个对象作为参数传递,也可以设置线程属性和获取线程ID。我们可以使用pthread_join()来等待线程终止。对于多线程编程,合理的控制线程并发和同步是很重要的。

原创文章,作者:KFXPO,如若转载,请注明出处:https://www.506064.com/n/362008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
KFXPOKFXPO
上一篇 2025-02-25 18:17
下一篇 2025-02-25 18:17

相关推荐

  • 深入解析Vue3 defineExpose

    Vue 3在开发过程中引入了新的API `defineExpose`。在以前的版本中,我们经常使用 `$attrs` 和` $listeners` 实现父组件与子组件之间的通信,但…

    编程 2025-04-25
  • 深入理解byte转int

    一、字节与比特 在讨论byte转int之前,我们需要了解字节和比特的概念。字节是计算机存储单位的一种,通常表示8个比特(bit),即1字节=8比特。比特是计算机中最小的数据单位,是…

    编程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什么是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一个内置小部件,它可以监测数据流(Stream)中数据的变…

    编程 2025-04-25
  • 深入探讨OpenCV版本

    OpenCV是一个用于计算机视觉应用程序的开源库。它是由英特尔公司创建的,现已由Willow Garage管理。OpenCV旨在提供一个易于使用的计算机视觉和机器学习基础架构,以实…

    编程 2025-04-25
  • 深入了解scala-maven-plugin

    一、简介 Scala-maven-plugin 是一个创造和管理 Scala 项目的maven插件,它可以自动生成基本项目结构、依赖配置、Scala文件等。使用它可以使我们专注于代…

    编程 2025-04-25
  • 深入了解LaTeX的脚注(latexfootnote)

    一、基本介绍 LaTeX作为一种排版软件,具有各种各样的功能,其中脚注(footnote)是一个十分重要的功能之一。在LaTeX中,脚注是用命令latexfootnote来实现的。…

    编程 2025-04-25
  • 深入剖析MapStruct未生成实现类问题

    一、MapStruct简介 MapStruct是一个Java bean映射器,它通过注解和代码生成来在Java bean之间转换成本类代码,实现类型安全,简单而不失灵活。 作为一个…

    编程 2025-04-25
  • 深入理解Python字符串r

    一、r字符串的基本概念 r字符串(raw字符串)是指在Python中,以字母r为前缀的字符串。r字符串中的反斜杠(\)不会被转义,而是被当作普通字符处理,这使得r字符串可以非常方便…

    编程 2025-04-25
  • 深入探讨冯诺依曼原理

    一、原理概述 冯诺依曼原理,又称“存储程序控制原理”,是指计算机的程序和数据都存储在同一个存储器中,并且通过一个统一的总线来传输数据。这个原理的提出,是计算机科学发展中的重大进展,…

    编程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一个程序就是一个模块,而一个模块可以引入另一个模块,这样就形成了包。包就是有多个模块组成的一个大模块,也可以看做是一个文件夹。包可以有效地组织代码和数据…

    编程 2025-04-25

发表回复

登录后才能评论