muduo库分析——base篇(8)ThreadLocal

TLS可以不同线程调用相同函数(相同的参数)获得不同的值


即使pkey[1]都是一样的,但是指向的内存数据不一样



 T& value()    //获得值的引用
  {
    T* perThreadValue = static_cast<T*>(pthread_getspecific(pkey_));    //通过getspecific获得储存的值的指针
    if (!perThreadValue)    //指针为空
    {
      T* newObj = new T();
      MCHECK(pthread_setspecific(pkey_, newObj));
      perThreadValue = newObj;
    }
    return *perThreadValue;    //返回其值
  }

猜你喜欢

转载自blog.csdn.net/renhaopeng/article/details/80225956
今日推荐