LINUX系统编程:原生线程库的封装

目录

1.前言

线程的局部存储

2.thread

构造函数

成员函数

3.线程库的封装


1.前言

在c++11之后,c++也支持了多线程,也就是thread库,thread库一定是对pthread库的一个封装,因为在Linux下不存在线程的概念,只有轻量级进程,pthread库就是对轻量级进程的系统调用接口封装成线程接口。

那么对线程的管理一定不是操作系统做的,因为操作系统不认识线程,这个工作是由pthread原生线程库做的。

pthread动态链接到共享区,使用struct pthread描述线程,struct pthread在共享区的起始地址就是该线程的 tid。

可以打印看看

#include<iostream>
#include<thread>
#include<pthread.h>
#include<unistd.h>
void handler(int num)
{
    sleep(1);
    std::cout << "I am new thread tid:" << pthread_self() << std::endl;
}

int main()
{
    std::thread t1(handler,10);
    std::cout<<"I am main thread tid:"<<pthread_self()<<std::endl;
    std::cout<<"I am main thread tid:"<<t1.get_id()<<std::endl;
    t1.join();
    return 0;
}

转化为16进制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值