/*
* =====================================================================================
*
* Filename:
*
* Description: Common function
*
* Version: 1.0
* Created: 2015年11月25日 16时11分13秒
* Revision: none
* Compiler: g++
*
* Author: betachen
* Company:
*
* =====================================================================================
*/ #ifndef __X__H__20151026__
#define __X__H__20151026__ #include <iostream>
#include <string>
#include <cstdarg>
#include <cstdio>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h> // MutexLock
class MutexLock
{
public:
MutexLock(): holder_(0)
{
pthread_mutex_init(&mutex_, NULL);
}
~MutexLock()
{
assert(holder_ == 0);
pthread_mutex_destroy(&mutex_);
}
bool isLockedBySelf() {return holder_ == pthread_self();}
void lock()
{
pthread_mutex_lock(&mutex_);
holder_ = pthread_self();
}
void unlock()
{
holder_ = 0;
pthread_mutex_unlock(&mutex_);
}
pthread_mutex_t* getPthreadMutex(){return &mutex_;} private:
MutexLock(const MutexLock& rx);
MutexLock& operator=(const MutexLock&); private:
pthread_mutex_t mutex_;
pthread_t holder_;
}; // MutexLockGuard
class MutexLockGuard
{
public:
explicit 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_;
}; //Condtion for threading
class Condition
{
public:
explicit Condition(MutexLock& mutex):mutex_(mutex)
{
pthread_cond_init(&cond_, NULL);
}
~Condition(){pthread_cond_destroy(&cond_);} void wait()
{
pthread_cond_wait(&cond_, mutex_.getPthreadMutex());
} void notify()
{
pthread_cond_signal(&cond_);
}
void notifyAll()
{
pthread_cond_broadcast(&cond_);
} private:
MutexLock& mutex_;
pthread_cond_t cond_;
}; //CountDownLatch
class CountDownLatch
{
public:
explicit CountDownLatch(int count):count_(count), cond_(mutex_){}
void wait()
{
MutexLockGuard lock(mutex_);
while (count_ > 0)
{
cond_.wait();
}
}
void countDown()
{
MutexLockGuard lock(mutex_);
count_--;
if (count_ == 0)
{
cond_.notifyAll();
}
}
private:
int count_;
mutable MutexLock mutex_;
Condition cond_; }; // FileHandle
class FileHandle
{
public:
FileHandle(const char* s, const char* sMode):file_name_(s)
{
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()
{
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_;
}; // has lock for threading
class Loger:public FileHandle
{
public:
Loger(const char* s):FileHandle(s, "a+"){}
Loger(const std::string& s):FileHandle(s, "a+"){}
void _(const char* sFile, const int iLine, const char* sFormat, ...)
{
va_list sList;
va_start(sList, sFormat);
gettimeofday(&tt, NULL);
ttt = localtime(&tt.tv_sec);
//time pid file line
fprintf(file_, "%04d%02d%02d-%02d:%02d:%02d.%03ld|%6d|%15s|% 5d|-> ",
ttt->tm_year+1900,ttt->tm_mon,ttt->tm_mday, ttt->tm_hour, ttt->tm_min, ttt->tm_sec,
tt.tv_usec/1000, getpid() ,sFile, iLine);
vfprintf(file_, sFormat, sList);
fprintf(file_, "\n");
va_end(sList);
}
void _(int lock_flag, const char* sFile, const int iLine, const char* sFormat, ...)
{
MutexLockGuard lock(lock_);
va_list sList;
va_start(sList, sFormat);
gettimeofday(&tt, NULL);
ttt = localtime(&tt.tv_sec);
fprintf(file_, "%04d%02d%02d-%02d:%02d:%02d.%03ld|%6d|%15s|% 5d|-> ",
ttt->tm_year+1900,ttt->tm_mon,ttt->tm_mday, ttt->tm_hour, ttt->tm_min, ttt->tm_sec,
tt.tv_usec/1000, getpid() ,sFile, iLine);
vfprintf(file_, sFormat, sList);
fprintf(file_, "\n");
va_end(sList);
} private:
Loger();
Loger(const Loger& rx);
Loger& operator = (const Loger& rx);
private:
struct tm* ttt;
struct timeval tt;
mutable MutexLock lock_;
}; #define NO_LOCK __FILE__,__LINE__
#define LOCK 1,__FILE__,__LINE__ //Loger log("webtest.log");
//#define _ log._ // To String
#include <algorithm>
template <typename T>
inline std::string toString(const T& value)
{
T i = value;
static const char* sDigits = "0123456789"; char buf[64] = "";
char* p = buf;
do{
int lsd = static_cast<int>(i % 10);
i /= 10;
*p++ = sDigits[lsd];
}while(i != 0); if (value < 0)
{
*p++ = '-';
}
*p = '\0';
std::reverse(buf, p); return buf;
} #endif

  

C++ RAII手法实例,不使用智能指针的更多相关文章

  1. enote笔记法使用范例(2)——指针(1)智能指针

    要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...

  2. 使用智能指针来管理对象 (基于RAII)

    ////一个简单的防止内存泄露的例子//void test() { //使用RAII的特性管理资源 //当智能指针unique_ptr被销毁时,它指向的对象也将被销毁 //这里test函数返回后 p将 ...

  3. 你应该掌握的C++ RAII手法:Scopegaurd

    C++作为一门Native Langueages,在C++98/03时代,资源管理是个大问题.而内存管理又是其中最大的问题.申请的堆内存需要手动分配和释放,为了确保内存正确释放,一般原则是" ...

  4. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  5. [5] 智能指针boost::shared_ptr

    [1]boost::shared_ptr简介 boost::shared_ptr属于boost库,定义在namespace boost中,包含头文件#include<boost/shared_p ...

  6. c++ 智能指针(转)

    智能指针的使用 智能指针是在 <memory> 标头文件中的 std 命名空间中定义的. 它们对 RAII 或“获取资源即初始化”编程惯用法至关重要. 此习惯用法的主要目的是确保资源获取与 ...

  7. RAII手法封装互斥锁

    RAII手法是 Resource Acquisition is Initialization 的缩写,意为“资源获取即初始化”,在使用智能指针时也使用,下面是针对互斥量时的实现, #include & ...

  8. Boost智能指针-基础知识

    简单介绍 内存管理一直是 C++ 一个比較繁琐的问题,而智能指针却能够非常好的解决问题,在初始化时就已经预定了删除.排解了后顾之忧.1998年修订的第一版C++标准仅仅提供了一种智能指针:std::a ...

  9. C++ 中的智能指针-基础

    简介 在现代 C++ 编程中,标准库包含了智能指针(Smart pointers). 智能指针用来确保程序不会出现内存和资源的泄漏,并且是"异常安全"(exception-safe ...

随机推荐

  1. JAVA_SE复习(多线程)

    线程 1.两种创建线程的方式都有自身的优点.  实现 Runnable 接口的优点:  从面向对象的设计观点看,Thread 类严格来讲是一个虚拟CPU 的封装,因此只有要改变或扩展该CPU 模型 ...

  2. CMake基础教程

    如果需要配置和检查我们工程中的所有依赖,那么可以选用CMake工具:但是,这并不是必须的,因为我们可以使用其他的工具或者是IDE(比如Makefiles或者Visual Studio)来配置我们的工程 ...

  3. JQuery的过滤选择器

    1.eg(num):查找索引num位置的元素,索引从0开始. 2.lt(num):查找索引小于num位置的元素,索引从0开始. 3.gt(num):查找索引大于num位置的元素,索引从0开始. 示例: ...

  4. 利用nginx做负载均衡

    round-robin:轮询.以轮询方式将请求分配到不同服务器上,默认 least-connected:最少连接数.将下一个请求分配到连接数最少的那台服务器上 ip-hash :基于客户端的IP地址. ...

  5. discuz 注册用户用到的几个表

    通过开启记录:mysql记录日志,筛出如下信息: INSERT INTO `userclub`.pre_ucenter_members SET secques='', username='pthlp1 ...

  6. 谈谈java中的WeakReference

    Java语言中为对象的引用分为了四个级别,分别为 强引用 .软引用.弱引用.虚引用. 本文只针对java中的弱引用进行一些分析,如有出入还请多指正. 在分析弱引用之前,先阐述一个概念:什么是对象可到达 ...

  7. C# asp Aspose.Cells 教程,包含增加勾选框,单元格,字体设置

    1,引用Aspose.Cells  dll 2,using Aspose.Cells; 3, Workbook excel = new Workbook(); string strFilePath = ...

  8. Microsoft Office Excel 不能访问文件

    问题描述: Microsoft Office Excel 不能访问文件“XX.xls”.可能的原因有: 1 文件名称或路径不存在.2 文件正被其他程序使用.3 您正要保存的工作簿与当前打开的工作簿同名 ...

  9. Critical Rendering Path

    1.生成 dom & cssom https://developers.google.com/web/fundamentals/performance/critical-rendering-p ...

  10. 防止IFRAME页被嵌套

    防止IFRAME页被嵌套 //最大化窗口,防止窗口嵌套 if (parent.location != window.location){ parent.location = window.locati ...