FROM:http://blog.csdn.net/wallwind/article/details/8136134
因为项目中需要用到主线程类共享所有数据,而主线程又要生成几个线程,而几个线程要用到主线程的数据,因此每个线程要保存一个主线程类指针,类似于自己的私有资源。因此就要用到这个类,主要是因为如果用多线程对同样的资源同步的话,就要对资源进行加锁、线程就要同步,那么同步就会涉及到线程的切换问题,而频繁切换就会导致cpu工作压力增大...,因此,根据情况利用适合的方法很重要......
下面开始介绍pthread_key_create的使用
-
#include <pthread.h>
-
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
还有
pthread_getspecific(), pthread_key_delete(),
几个函数
对其进行封装,每次调用设置全局变量就OK
下面cpp代码仅供学习
mytls.h
-
/**
-
create by wallwind
-
mail:wochenglin@qq.com
-
2012/11/1
-
-
**/
-
-
#ifndef _MyTLS_h__
-
#define _MyTLS_h__
-
-
#include <pthread.h>
-
enum {NAME_SIZE =20,THREAD_NUM=10,};
-
-
//typedef DWORD pthread_key_t;
-
const pthread_key_t INVALID_PTHREAD_KEY = (pthread_key_t)0xFFFFFFFF;
-
//typedef void (*destory_callback)(void*);
-
//MyTLSPtr必须为进程全局变量,并且在初始化时直接申请key,而不是在第一次set时使用once方式来初始化key
-
//暂时不使用线程关闭时自动释放的方式,仍然使用谁创建谁释放的原则
-
-
class MyTLSPtr
-
{
-
public:
-
MyTLSPtr():m_key(INVALID_PTHREAD_KEY)
-
{
-
init();
-
}
-
virtual ~MyTLSPtr()
-
{
-
destory();
-
}
-
public:
-
-
template< typename T >
-
void set(T* pPtr)
-
{
-
pthread_setspecific(m_key, pPtr);
-
}
-
-
template< typename T >
-
T* get() const
-
{
-
return static_cast<T*>(pthread_getspecific(m_key));
-
}
-
-
void init()
-
{
-
if(m_key != INVALID_PTHREAD_KEY)
-
{
-
destory();
-
}
-
-
pthread_key_create(&m_key, NULL);
-
}
-
-
void destory()
-
{
-
if(m_key == INVALID_PTHREAD_KEY)
-
return;
-
-
pthread_key_delete(m_key);
-
-
m_key = INVALID_PTHREAD_KEY;
-
}
-
-
private:
-
pthread_key_t m_key;
-
//destory_callback m_pCallBack;
-
};
-
-
template< typename T >
-
class MyTLSTypePtr : public MyTLSPtr
-
{
-
public:
-
MyTLSTypePtr()
-
:MyTLSPtr()
-
{}
-
virtual ~MyTLSTypePtr()
-
{}
-
-
public:
-
void set(T* pPtr)
-
{
-
MyTLSPtr::set<T>(pPtr);
-
}
-
-
T* get() const
-
{
-
return MyTLSPtr::get<T>();
-
}
-
};
-
-
#endif // MyTLS_h__
-
//create by wallwind mail:wochenglin@qq.com 2012/11/1
-
-
#include <malloc.h>
-
#include <pthread.h>
-
#include <stdio.h>
-
-
#include "MyTLS.h"
-
//enum {NAME_SIZE =20,THREAD_NUM=10,};
-
-
MyTLSPtr g_TLS;
-
-
void write_to_thread_log (const char* message);
-
-
void* thread (void* args)
-
{
-
char log[NAME_SIZE];
-
FILE* file;
-
sprintf(log,"thread_%d.log",(int)pthread_self());
-
-
file = fopen (log, "w");
-
g_TLS.set(file);
-
-
write_to_thread_log ("message test");
-
}
-
-
-
void write_to_thread_log (const char* message)
-
{
-
-
FILE* thread_log = g_TLS.get<FILE>();
-
fprintf (thread_log, "%s\n", message);
-
-
}
-
int main()
-
{
-
int i;
-
pthread_t threads[THREAD_NUM];
-
-
for(i = 0; i<THREAD_NUM;i++) //创建线程
-
pthread_create(&threads[i],NULL,thread,NULL);
-
-
for(i = 0;i<THREAD_NUM;i++) //主线程阻塞生成线程
-
-
pthread_join(threads[i],NULL);
-
return 0;
-
}
生成的文件为
同理c代码如下(效果同上cpp文件):
-
#include <stdio.h>
-
#include <pthread.h>
-
#include <malloc.h>
-
-
static pthread_key_t thread_log_key;
-
-
void write_to_thread_log( char const * message )
-
{
-
FILE * thread_log = ( FILE * )pthread_getspecific( thread_log_key );
-
fprintf( thread_log, "%s/n", message );
-
}
-
-
void close_thread_log( void * thread_log )
-
{
-
fclose( ( FILE * )thread_log );
-
}
-
-
void * thread_function( void * args )
-
{
-
char thread_log_filename[ 20 ];
-
FILE* thread_log;
-
sprintf( thread_log_filename, "thread%d.log", ( int )pthread_self() );
-
thread_log = fopen( thread_log_filename, "w" );
-
pthread_setspecific( thread_log_key, thread_log );
-
write_to_thread_log( "Thread starting." );
-
return NULL;
-
}
-
-
int main()
-
{
-
int i;
-
pthread_t threads[5];
-
pthread_key_create( &thread_log_key, close_thread_log );
-
for ( i = 0; i < 5; ++i )
-
{
-
pthread_create( &(threads[i]), NULL, thread_function, NULL );
-
}
-
for ( i = 0; i < 5; ++i )
-
{
-
pthread_join( threads[i], NULL );
-
}
-
return 0;
-
}
阅读(1562) | 评论(0) | 转发(0) |