多线程安全

项目中需要在多线程环境下,输出日志到标准输出,以下是实现过程。

首先,我们需要一个锁类,能够自动初始化,并且降低耦合。

/*
 * locker.h
 *
 *  Created on: Apr 14, 2012
 *      Author: joan
 */

#ifndef LOCKER_H_
#define LOCKER_H_

#include "../OPTION.h"

class locker
{
public:
	inline locker(){		pthread_mutex_init(&mutex,NULL);}
	inline ~locker(){		pthread_mutex_destroy(&mutex);}
	inline void lock(){		pthread_mutex_lock(&mutex);}
	inline void unlock(){	pthread_mutex_unlock(&mutex);}
private:
	pthread_mutex_t mutex;
};

#endif /* LOCKER_H_ */

其次,声明日志类,重点是将构造函数私有化,将函数成员和数据成员声明为静态,添加实例指针和全局访问点。

/*
 * log.h
 *
 *  Created on: Apr 8, 2012
 *      Author: joan
 */

#ifndef LOG_H_
#define LOG_H_

#include "../OPTION.h"
#include "locker.h"

/*
 * this class is responsible for the running log of tinyJSE
 * there should only exist one instance of tinyLog,
 * so we use singleton to implement tinyLog
 */
class tinyLog
{
public:
	static tinyLog *GetInstance();
	static void WriteLog(const char *FORMAT,...);
private:
	tinyLog();
	~tinyLog();
private:
        static tinyLog *log;
static locker llock;
};
#endif /* LOG_H_ */
然后是日志类的实现,注意全局访问点中使用double check提高性能。

/*
 * log.cpp
 *
 *  Created on: Apr 8, 2012
 *      Author: joan
 */

#include "../OPTION.h"
#include "log.h"

tinyLog * tinyLog::log = NULL;
locker tinyLog::llock;

tinyLog::tinyLog()
{
}

tinyLog::~tinyLog()
{
}

/*
 * get the pointer to the only instance of tinyLog
 * use double check to assure only one instance is created
 */
tinyLog *tinyLog::GetInstance()
{
	if(NULL == log)
	{//double check
		llock.lock();
		if(NULL == log)
		{
			log = new tinyLog();
		}
		llock.unlock();
	}
	return log;
}

/*
 * Unified handling of the log of tinyJSE
 */
void tinyLog::WriteLog(const char *FORMAT,...)
{
	va_list args;

	va_start(args, FORMAT);

	llock.lock();

	vfprintf(stdout,FORMAT,args);

	llock.unlock();

	va_end(args);

}

使用该单例:

#define PRINT(FORMAT,args...)	tinyLog::GetInstance()->WriteLog(FORMAT,##args)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值