关闭

FSBII(八)class KMutex

389人阅读 评论(0) 收藏 举报
//---------------------------------------------------------------------------
// Sword3 Engine (c) 1999-2000 by Kingsoft
//
// File:	KMutex.h
// Date:	2000.08.08
// Code:	WangWei(Daphnis)
// Desc:	Header File
//---------------------------------------------------------------------------
#ifndef KMutex_H
#define KMutex_H
//---------------------------------------------------------------------------
#define SINGLE_PROCESS
//---------------------------------------------------------------------------
#ifdef WIN32
class ENGINE_API KMutex
#else
class KMutex
#endif
{
private:
#ifdef WIN32
#ifdef SINGLE_PROCESS
	CRITICAL_SECTION m_CriticalSection;//用于单进程的线程同步
#else
	HANDLE m_hMutex;//用于多进程的线程同步
#endif
#else
    pthread_mutex_t mutex;
#endif
public:
    KMutex();
    ~KMutex();
    void Lock(void);
    void Unlock(void);
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
// Sword3 Engine (c) 1999-2000 by Kingsoft
//
// File:	KMutex.cpp
// Date:	2000.08.08
// Code:	WangWei(Daphnis)
// Desc:	Implements a simple mutex object for thread synchronization
//---------------------------------------------------------------------------
#include "KWin32.h"
#include "KDebug.h"
#include "KMutex.h"

KMutex::KMutex()
{
#ifdef WIN32
#ifdef SINGLE_PROCESS
	InitializeCriticalSection(&m_CriticalSection);
#else
	m_hMutex = CreateMutex(NULL, FALSE, NULL);
	if (!m_hMutex)
		g_MessageBox("KMutex::KMutex() CreateMutex() failed!");
#endif
#else
     int rc = pthread_mutex_init(&mutex, NULL);
#endif
}
//---------------------------------------------------------------------------
// 函数:	~KMutex
// 功能:	析购函数
// 参数:	void
// 返回:	void
//---------------------------------------------------------------------------
KMutex::~KMutex()
{
#ifdef WIN32
#ifdef SINGLE_PROCESS
	DeleteCriticalSection(&m_CriticalSection);
#else
	CloseHandle(m_hMutex);
#endif
#else
     int rc = pthread_mutex_destroy(&mutex);
#endif
}
//---------------------------------------------------------------------------
// 函数:	Lock
// 功能:	锁定
// 参数:	void
// 返回:	void
//---------------------------------------------------------------------------
void KMutex::Lock(void)
{
#ifdef WIN32
#ifdef SINGLE_PROCESS
	EnterCriticalSection(&m_CriticalSection);
#else
	WaitForSingleObject(m_hMutex, INFINITE);
#endif
#else
     int rc = pthread_mutex_lock(&mutex);
#endif
}
//---------------------------------------------------------------------------
// 函数:	Unlock
// 功能:	解开
// 参数:	void
// 返回:	void
//---------------------------------------------------------------------------
void KMutex::Unlock(void)
{
#ifdef WIN32
#ifdef SINGLE_PROCESS
	LeaveCriticalSection(&m_CriticalSection);
#else
	ReleaseMutex(m_hMutex);
#endif
#else
     int rc = pthread_mutex_unlock(&mutex);
#endif
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:122070次
    • 积分:1697
    • 等级:
    • 排名:千里之外
    • 原创:47篇
    • 转载:19篇
    • 译文:0篇
    • 评论:23条
    文章分类
    最新评论