muduo库 CurrenThread

#pragma once

#include <unistd.h>
#include <sys/syscall.h>

namespace CurrentThread
{
    extern __thread int t_cachedTid;

    void cacheTid();

    inline int tid()
    {
        if (__builtin_expect(t_cachedTid == 0, 0))
        {
            cacheTid();
        }
        return t_cachedTid;
    }
}
#include"CurrentThread.h"

namespace CurrentThread
{
    __thread int t_cachedTid = 0;

    void cacheTid()
    {
        if(t_cachedTid==0)
        {
            //
            t_cachedTid=static_cast<pid_t>(::syscall(SYS_gettid));
        }
    }
}

版权声明:本文为weixin_50911232原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。