FSBII(八)class KMutex

//---------------------------------------------------------------------------
// 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值