加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

跨平台C++ 锁和信号量 mutex.cpp

(2005-05-26 11:05:14)
标签:

杂谈

分类: 计算机与 Internet

#include "mutex.h"

#ifndef WIN32
 static pthread_mutexattr_t  *sMutexAttr=NULL;
 static void MutexAttrInit();
 static pthread_once_t sMutexAttrInit = PTHREAD_ONCE_INIT;
#endif

MMutex::MMutex()
{
#ifdef WIN32
    ::InitializeCriticalSection(&fMutex);
#else
    (void)pthread_once(&sMutexAttrInit, MutexAttrInit);
    (void)pthread_mutex_init(&fMutex, sMutexAttr);
#endif
}

#ifndef WIN32
void MutexAttrInit()
{
    sMutexAttr = (pthread_mutexattr_t*)malloc(sizeof(pthread_mutexattr_t));
    ::memset(sMutexAttr, 0, sizeof(pthread_mutexattr_t));
    pthread_mutexattr_init(sMutexAttr);
}
#endif

MMutex::~MMutex()
{
#ifdef WIN32
    ::DeleteCriticalSection(&fMutex);
#else
    pthread_mutex_destroy(&fMutex);
#endif
}

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有