线程局部存储空间 pthread_key_t、__thread 即 ThreadLocal
https://www.jianshu.com/p/495ea7ce649b?utm_source=oschina-app
该博客还未学习完 还有 pthread_key_t Thread_local
__thread 修饰的变量
- __thread是GCC内置的线程局部存储设施,__thread变量每一个线程有一份独立实体,各个线程的值互不干扰。可以用来修饰那些带有全局性且值可能变,但是各线程独立不干扰的变量;
- 只能修饰POD类型(类似整型指针的标量),不能修饰class类型,因为无法自动调用构造函数和析构函数;
- 可以用于修饰全局变量,函数内的静态变量,不能修饰函数的局部变量或者class的普通成员变量;
- 且__thread变量值只能初始化为编译器常量。
#include <pthread.h>
#include <cstdio>
#include <cstdlib>
#include <assert.h>
#include <stdint.h> __thread uint64_t pkey = ; void run2( )
{
FILE* fp = NULL;
cout<<"thread1 run2 thread = "<<pkey<<endl;//有值了 if( !pkey )
{
char fName[] = "";
sprintf( fName, "thread%lu.log", static_cast<unsigned long>( pthread_self() ) );
fp = fopen( fName, "w" );
pkey = reinterpret_cast<uint64_t>( fp ); }else fp = reinterpret_cast<FILE*>( pkey ); fprintf( fp, "hello __thread 2\n" );
return ;
} void* run1( void* arg )
{
FILE* fp = NULL;
cout<<"thread1 run1 thread = "<<pkey<<endl;// if( !pkey )
{
char fName[] = "";
sprintf( fName, "thread%lu.log", static_cast<unsigned long>( pthread_self() ) );
fp = fopen( fName, "w" );
pkey = reinterpret_cast<uint64_t>( fp ); }else fp = reinterpret_cast<FILE*>( pkey ); fprintf( fp, "hello __thread 1\n" ); run2(); return NULL;
} int main(int argc, char const *argv[])
{
char fName[] = "";
sprintf( fName, "thread%lu.log", static_cast<unsigned long>( pthread_self() ) );
FILE* fp = fopen( fName, "w" );
cout<<"main thread = "<<pkey<<endl;// pkey = reinterpret_cast<uint64_t>( fp );
fprintf( fp, "hello __thread\n" );
cout<<"main thread end = "<<pkey<<endl;///其他值 pthread_t threads[];
pthread_create( &threads[], NULL, run1, NULL );//开启线程
pthread_create( &threads[], NULL, run1, NULL );
pthread_join( threads[], NULL );
pthread_join( threads[], NULL );
return ;
} main thread =
main thread end =
thread1 run1 thread =
thread1 run2 thread =
thread1 run1 thread =
thread1 run2 thread = 一共创建了 3个文件 分别对应三个线程
pthread_key_t
pthread_key_t 优于 __thread 从下面几个方面来说:
- 依赖 linux 环境的 libpthread, 而非 gcc 编译器可移植性增强
- 如上所示,可以认为对每个 pthread_key, 库内部提供了一个 __thread void* 接受 pthread_setspecific 设置的指针,从而可以指向 class 类型
- pthread_key_t 可以作为函数的局部变量,也可以作为局部变量
#include <pthread.h>
// pthread_key_t, pthread_setspecific, pthread_getspecific, pthread_self
// pthread_key_create, pthread_key_delete, pthread_create, pthread_join
#include <iostream>
#include <cstdio>
#include <cstdlib> using namespace std; static pthread_key_t pkt;
// 1, callback function to destroy resource associated with key
// 2, the in_param is pthread_getspecific()
// 3, gettid()是内核给线程(轻量级进程)分配的进程id,全局(所有进程中)唯一
// 4, pthread_self()是在用户态实现的,获取的id实际上是主线程分配给子线程的线程描述符的地址而已,只是在当前进程空间中是唯一的。
void destroy( void *arg )
{
printf("exit at thread %d, fclose file \n", static_cast<int>( pthread_self() ) );
if( arg ) fclose( reinterpret_cast<FILE*>(arg) );
}
// 5, pthread_getspecific() Return current value of the thread-specific data slot identified by KEY.
void writeLog( const char* log )
{
FILE* logHandle = reinterpret_cast<FILE*>( pthread_getspecific( pkt) );
fprintf( logHandle, "%s\n", log );
}
// 6, pthread_setspecific Store POINTER in the thread-specific data slot identified by KEY
void* work( void* arg)
{
FILE* logHandle = NULL;
char fileName[128] = "";
sprintf( fileName, "Thread%d.log", static_cast<int>(pthread_self()) );
logHandle = fopen( fileName, "w");
pthread_setspecific( pkt, reinterpret_cast<void*>( logHandle ) );
writeLog( "Thread starting." );
}
// 7, pthread_key_create( &pkt, destroy ) Create a key value identifying a location in the thread-specific //identifying 识别
// data area. Each thread maintains a distinct thread-specific data area.
// the destroy callback function will called with the key is dectroyed
// 8, pthread_key_delete( ) detroy the key use callback function clear the resource
int main(int argc, char const *argv[])
{
pthread_key_create( &pkt, destroy );
pthread_t pids[2] = {0};
pthread_create( &pids[0], NULL, work, NULL );
pthread_create( &pids[1], NULL, work, NULL );
pthread_join( pids[0], NULL );
pthread_join( pids[1], NULL );
pthread_key_delete( pkt );
printf("stop\n");
return 0;
}
ThreadLocal
对 pthread_key_t 进行了 RAII 的封装,使用更加安全。
#include <pthread.h>
#include <boost/noncopyable.hpp> // noncopyable
#include <boost/checked_delete.hpp> // check_delete
#include <cstdio>
#include <cstdlib>
#include <string>
#include <stdexcept> template<typename T>
class ThreadLocal : public boost::noncopyable
{
public:
typedef ThreadLocal<T>* pThreadLocal;
ThreadLocal()
{ pthread_key_create( &pkey_, &ThreadLocal::destroy ); } ~ThreadLocal()
{ pthread_key_delete( pkey_ ); } T& value()
{
T* pvalue = reinterpret_cast<T*>( pthread_getspecific( pkey_ ) );
if( !pvalue )
{
T* obj = new T();
pthread_setspecific( pkey_, reinterpret_cast<void*>( obj ) );
pvalue = obj;
}
return *pvalue;
} private:
static void destroy( void* arg )
{
T* obj = reinterpret_cast<T*>( arg );
boost::checked_delete( obj );
} pthread_key_t pkey_;
}; class Logger
{
public:
Logger()
{
char fName[128] = "";
sprintf( fName, "log_%lu.log", static_cast<unsigned long>( pthread_self() ) );
fp = fopen( fName, "w" );
if( !fp ) throw std::runtime_error( std::string("can not create ") + fName );
} ~Logger() { fclose( fp ); } void log( const std::string& s ) { fprintf( fp, "%s\n", s.c_str() ); } private:
FILE* fp;
}; void* run( void* arg )
{
auto ptllogger = reinterpret_cast< ThreadLocal<Logger>::pThreadLocal>( arg);
Logger& plogger = ptllogger->value();
plogger.log( "Hello thread local" );
} int main()
{
ThreadLocal<Logger>::pThreadLocal p = new ThreadLocal<Logger>;
Logger& plogger = p->value();
plogger.log( "Hello thread local" ); pthread_t threads[2] = {0};
pthread_create( &threads[0], NULL, run, reinterpret_cast<void*>( p ) );
pthread_create( &threads[1], NULL, run, reinterpret_cast<void*>( p ) );
pthread_join( threads[0], NULL );
pthread_join( threads[1], NULL );
delete p;
}
需要 打开原始连接 看看
线程局部存储空间 pthread_key_t、__thread 即 ThreadLocal的更多相关文章
- 并发基础(十) 线程局部副本ThreadLocal之正解
本文将介绍ThreadLocal的用法,并且指出大部分人对ThreadLocal 的误区. 先来看一下ThreadLocal的API: 1.构造方法摘要 ThreadLocal(): 创建一个线程 ...
- RocksDB线程局部缓存
概述 在开发过程中,我们经常会遇到并发问题,解决并发问题通常的方法是加锁保护,比如常用的spinlock,mutex或者rwlock,当然也可以采用无锁编程,对实现要求就比较高了.对于任何一个共享变量 ...
- 线程封闭之栈封闭和ThreadLocal
线程封闭 在多线程的环境中,我们经常使用锁来保证线程的安全,但是对于每个线程都要用的资源使用锁的话那么程序执行的效率就会受到影响,这个时候可以把这些资源变成线程封闭的形式. 1.栈封闭 所谓的栈封闭其 ...
- Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离。
Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离.
- 4、线程范围内的数据共享之ThreadLocal
/** * 线程范围类的数据共享 * 核心:ThreadLocal类 * 实际场景: * Hibernate的getCurrentSession方法,就是从线程范围内获取存在的session,如果不存 ...
- Java线程和多线程(七)——ThreadLocal
Java中的ThreadLocal是用来创建线程本地变量用的.我们都知道,访问某个对象的所有线程都是能够共享对象的状态的,所以这个对象状态就不是线程安全的.开发者可以通过使用同步来保证线程安全,但是如 ...
- 线程范围内的环境变量---ThreadLocal
package cn.itcast.heima2; import java.util.HashMap; import java.util.Map; import java.util.Random; p ...
- 过虑器 ThreadLocal 权限 监听器 观察者模式
数据的压缩 GzipOutputStream - > > ByteArrayOutputStream. 以下是在某个servlet中对指定的数据进行压缩 package cn.itcast ...
- 用ThreadLocal管理事务
1.适用场景 一个service,操作两个dao,要求两个dao为同一个事务,要么全成功,要么全失败.
随机推荐
- QT学习之多线程
[为什么要用多线程?] 传统的图形用户界面应用程序都只有一个执行线程,并且一次只执行一个操作.如果用户从用户界面中调用一个比较耗时的操作,当该操作正在执行时,用户界面通常会冻结而不再响应.这个问题可以 ...
- 团体程序设计天梯赛L1-023 输出GPLT 2017-03-22 17:56 39人阅读 评论(0) 收藏
L1-023. 输出GPLT 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个长度不超过10000的.仅由英文字母构成的 ...
- 配置hive环境以及mysql配置后必须做
1.先在主节点上安装阿里云配置(看别的文档) 2.把需要的两个jar包加入进来(放到hadoop用户目录下面即可即/home/hadoop/) mysql-connector-java-5.1.47. ...
- [Windows] IIS7.5 部署ISAPI
环境: OS:Windows Server 2008 R2 Enterprise sp1 64位 IIS:7.5 ISAPI: delphi xe 编译的webbroker isapi dll 3 ...
- EDM模板制作规范
为了保证最大的兼容性,在制作HTML的email页面时,请严格按照规范来书写: 1.页面宽度推荐500px,最大不要超过750px: 2.制作HTML的email页面时,不使用css+div来布局,最 ...
- c#实现高斯模糊
说说高斯模糊 高斯模糊的理论我这里就不太多费话了,百度下太多,都是抄来抄去. 主要用到二个函数“高斯函数” 一维形式为: 二维形式为: X,Y对应的一维二维坐标,σ表示模糊半径(半径* 2 + 1) ...
- Django Query
Making Qeries 一旦创建了数据模型,Django就会自动为您提供一个数据库抽象API,允许您创建.检索.更新和删除对象.本文档解释了如何使用这个API. The models 一个clas ...
- 「HNOI 2015」落忆枫音
题目链接 戳我 \(Description\) 给一张\(n\)割点\(m\)条边的\(DAG\),保证点\(1\)不存在入边,现在需要在\(DAG\)中加入一条不在原图中的边\((x,y)\),求这 ...
- lua之base64加密和解密算法。
local function encodeBase64(source_str) local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop ...
- AOP之 Filter实用
前言 开心一笑~~~ 一个年轻的程序员和一个项目经理登上了一列在山里行驶的火车,他们发现列车上几乎都坐满了,只有两个在一起的空位,这个空位的对面是一个老奶奶和一个年轻漂亮的姑娘.两个上前坐了下来.程序 ...