pthread入门

pthread是linux下C语言执行多线程操作的基础,操作多线程的操作全部在pthread.h头文件中,因此想要创建一个线程并且操作线程的话,就需要引入头文件,跟线程密切相关的操作包括创建、退出以及主线程的等待(主线程一般是指main函数)

一、创建线程

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

函数的第一个参数返回的是一个线程的指针,该指针唯一的标识创建的线程。
参数attr用来设置线程的属性,一般我们设置为NULL,即采用系统的默认设置。
第三个参数是线程将要执行的方法,或者说是线程的主体,该参数是一个函数指针,参数和返回值都是void*类型
第四个参数是第三个参数代表的方法所需要的参数

当创建线程成功时,该方法返回一个不为0的int

二、离开线程(销毁线程)

void pthread_exit(void* retval)

该方法离开该线程,并且返回一个指向某个对象的指针(该指针不能用来指向一个局部变量,因此离开线程之后,线程被销毁,资源被释放)

三、等待线程

在绝大多数情况下,我们需要在主线程中等待子进程的完成,然后执行后续操作,这样我们就需要pthread_join函数来完成任务。

int pthread_join(pthread_t t,void** thread_return)

该方法类似于fork中的wait,但不同的是,该方法面向的对象是线程而非进行,主线程会一直挂起,直到关注的子线程返回。
参数t表示关注的子线程的唯一标识
参数thread_return是一个void*值的地址,该参数得到的是线程的返回数据,即为pthred_exit中的参数值

四、DEMO

例子在主线程中创建一个线程,然后等待子线程的完成,并且在子线程中操作修改了一个全局变量message,然后等子线程返回后在重新调用message的值,程序的完成代码如下

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

void* thread_function(void* arg);
char message[]="hello world!\n";

int main(){

    int res;
    pthread_t a_thread;
    void* thread_result;

    res=pthread_create(&a_thread,NULL,thread_function,(void*)message);

    if(res!=0){
        printf("something wrong with the create thread\n");
        exit(EXIT_FAILURE);
    }

    printf("waiting for thread to finish..\n");

    res=pthread_join(a_thread,&thread_result);

    /**
    printf("res is %d\n",res);
    **/
    if(res!=0){
        printf("something wrong with the join\n");
        exit(EXIT_FAILURE);
    }

    printf("thread joined, it returns %s\n",(char*)thread_result);
    printf("message now is %s\n",message);
    exit(EXIT_SUCCESS);
}


void* thread_function(void* arg){
    printf("thread function is now runing1,the argument is %s\n",(char*)arg);
    sleep(3);
    strcpy(message,"Bye");
    pthread_exit("Thank you for the CPU time!\n");
}

注:linux下编译此程序需要执行下面的shell脚本

gcc simpleThread.c -o simpleThread -lpthread

程序的运行结果如下所示
这里写图片描述

备注:

在该段代码中,我们的共享变量是char[]类型,如果我们把message的类型改成char*,那么这段代码在运行的时候会发生段错误,因为char*指向的是一段常量字符串,该值不可被改变,但是char[]所表示的指针指向一段数组,只要不越界,就可进行操作,这里有一个很经典的笔试题

char* s1="hi";
char* s2="hi";
char s3[]="hi";
char s4[]="hi";

s1==s2,s3!=s4

同样,我们可以考虑一下c++ stl库中的string类型,在其内部究竟是维护了一个char*类型还是char[]类型?
当然是char[],但是用char*去特指,即在创建的时候申请一个char数组,然后把数组指针赋给char*,那么我们在对字符串进行扩容或者执行+操作是,肯定会额外再去申请一段数组,因此如果我们把数组的大小,使用程度存储起来的话,就能在一定程度上减少申请操作,这就是动态字符串的原理,redis等kv数据库均采用这种方法存储字符串

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pthread和std::thread是两种不同的线程库。引用指出,pthread是C语言中使用的线程库,而std::thread是C++11标准库中包含的对线程的支持。 一个显著的区别是在创建线程时的用法。引用中提到,对于pthread,需要使用pthread_create函数来指定线程执行的函数,并将参数打包成结构体进行传递。而对于std::thread,创建一个线程对象后,线程会立即执行,不需要显式调用start或run函数。 另一个区别是在编程风格上。pthread是C语言的库,在使用时需要手动管理线程的生命周期、线程间的同步等。而std::thread是C++标准库的一部分,它提供了更高级的抽象,自动处理线程的创建、销毁以及线程间的同步操作,使得编程更加方便和易于理解。 总之,pthread是一个底层的、面向C语言的线程库,而std::thread是C++11标准库中提供的更高级的、面向C++的线程库。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++11多线程std::thread入门使用以及对比分析pthread](https://blog.csdn.net/m0_37251750/article/details/126409127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++多线程pthreadthread](https://blog.csdn.net/natureworld2010/article/details/108501774)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值