线程编程 -- __thread

本文深入探讨了GCC内置的线程局部存储__thread特性,解释了其定义、使用规则及应用场景,通过示例展示了如何利用__thread变量保存线程上下文信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程编程 – __thread

一. __thread 定义

__thread是GCC内置的线程局部存储设施,存取效率可以和全局变量相比。
__thread变量每一个线程有一份独立实体,各个线程的值互不干扰。可以用来修饰那些带有全局性且值可能变,但是又不值得用全局变量保护的变量。

二. _thread使用规则

只能修饰POD类型(类似整型指针的标量,不带自定义的构造、拷贝、赋值、析构的类型,二进制内容可以任意复制memset,memcpy,且内容可以复原)
  不能修饰 class 类型,因为无法自动调用构造函数和析构函数,可以用于修饰全局变量,函数内的静态变量,不能修饰函数的局部变量或者class的普通成员变量,且__thread变量值只能初始化为编译期常量,即编译期间就能确定值。

三. 使用举例

场景说明:每个线程有一些需要保存的上下文信息,即可使用 __thread 变量

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <syscall.h>
#include <pthread.h>
#include <string>
#include <thread>
using namespace std;

namespace thread_context
{
__thread int t_tid = 0; //线程id
__thread char t_tidString[32] = ""; //线程id字符串
__thread int t_tidLen = 0; //字符串长度
__thread const char *t_threadName = "unknown"; //线程名

pid_t gettid()
{
    return static_cast<pid_t>(::syscall(SYS_gettid));
}

  //缓存线程id字符串
void catchTid()
{
    if (t_tid == 0)
    {
        t_tid = gettid();
        t_tidLen = snprintf(t_tidString, sizeof t_tidString, "%5d", t_tid);
    }
}

int tid()
{
    if (__builtin_expect(t_tid == 0, 0))
    {
        catchTid();
    }
    return t_tid;
}

int tidLen()
{
    return t_tidLen;
}

string tidString()
{
    return t_tidString;
}

string name()
{
    return t_threadName;
}

bool isMainThread()
{
    return tid() == ::getpid();
}

}; // namespace thread_context
int main()
{
    if (thread_context::isMainThread())
        printf("this is main thread\n");
    else
        printf("this is not main thread tid : %d\n ", thread_context::tid());

    auto t = std::thread([] {
        if (thread_context::isMainThread())
            printf("this is main thread");

        else
            printf("this is not main thread tid : %d\n ", thread_context::tid());
    });
    t.join();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值