码迷,mamicode.com
首页 > 编程语言 > 详细

C++ RAII手法实现的线程安全的日志文件写例子

时间:2015-10-27 01:42:25      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdarg>
#include <cstdio>
#include <pthread.h>


// MutexLock 封装互斥锁的接口
class MutexLock
{
public:
    MutexLock()
    {
        pthread_mutex_init(&mutex_, NULL);
    }
    MutexLock(const MutexLock& rx)
    {
        mutex_ = rx.mutex_;
    }
    ~MutexLock()
    {
        pthread_mutex_destroy(&mutex_);
    }
    void lock(){ pthread_mutex_lock(&mutex_);}
    void unlock(){ pthread_mutex_unlock(&mutex_);}

private:
    MutexLock& operator=(const MutexLock&);

private:
    pthread_mutex_t mutex_;
};

// MutexLockGuard  RAII手法的进入退出加锁解锁
class MutexLockGuard
{
public:
    MutexLockGuard(MutexLock& lock):mutex_lock_(lock)
    {
        mutex_lock_.lock();
    }
    ~MutexLockGuard()
    {
        mutex_lock_.unlock();
    }

private:
    MutexLockGuard();
    MutexLockGuard(const MutexLockGuard& rx);
    MutexLockGuard& operator=(const MutexLockGuard& rx);

private:
    MutexLock& mutex_lock_;
};


// FileHandle  RAII手法的文件句柄类
class FileHandle  
{
public:
    FileHandle(const char* s, const char* sMode):file_name_(s)
    {
        std::cout<<file_name_<<" opened"<<std::endl;
        file_ = fopen(s, sMode);
        if (file_ == NULL)
        {
            std::cerr<<s<<" open failed"<<std::endl;    
        }
    }
    FileHandle(const std::string& s, const char* sMode):file_name_(s)
    {
        std::cout<<file_name_<<" opened"<<std::endl;
        file_ = fopen(s.c_str(), sMode);
        if (file_ == NULL)
        {
            std::cerr<<s<<" open failed"<<std::endl;    
        }
    }
    ~FileHandle()
    {
        std::cout<<file_name_<<" closed"<<std::endl;
        if (file_)
            fclose(file_);
    }
    FILE* get(){return file_;}
    std::string&  get_name(){return file_name_;};

private:
    FileHandle();
    FileHandle(const FileHandle& rx);
    FileHandle& operator = (const FileHandle& rx);

protected:
    FILE* file_;
    std::string  file_name_;
};


// need lock for threading 文件写类,为了兼容c语言风格所以选择了stdio.h
class Loger:public FileHandle   
{
public:
    Loger(const char* s):FileHandle(s, "a+"){}
    Loger(const std::string& s):FileHandle(s, "a+"){}
    void _(const char* sFormat, ...)
    {
        MutexLockGuard lock(lock_);
        va_list sList;
        va_start(sList, sFormat);
        vfprintf(file_, sFormat, sList);
        fprintf(file_, "\n");
        fflush(file_);
        va_end(sList);
    }

private:
    Loger();
    Loger(const Loger& rx);
    Loger& operator = (const Loger& rx);
private:
    mutable MutexLock lock_;
}; 

 

// main 函数读写测试

void* tt(void*)
{
    vector<int> vInt(100);
    int i = 0;
    for(vector<int>::iterator iter = vInt.begin(); iter != vInt.end(); ++iter)
    {
        *iter = i++;
        log._("[%lu]--->[%d]", pthread_self(),*iter);
    }
}

Loger log("/home/chenhao/appx/src/cpp/tt");

int main()
{
    pthread_t tid1, tid2, tid3;

    pthread_create(&tid1, NULL, tt, NULL);
    pthread_create(&tid2, NULL, tt, NULL);
    pthread_create(&tid3, NULL, tt, NULL);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
    log._("----------");
        return 0;  
    
}

 

技术分享

 

C++ RAII手法实现的线程安全的日志文件写例子

标签:

原文地址:http://www.cnblogs.com/clor001/p/4912932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!