⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yslock.h

📁 电力故障信息采集,主要是针对南自的保护装置,这个程序用在Linux操作系统下
💻 H
字号:
// YsLock.h: interface for the YsLock class.
//
//////////////////////////////////////////////////////////////////////

/**

 *

 * 文 件 名	: YsLock.h

 * 创建日期	: 2006-09-10

 * 作    者 : 邵凯田(skt001@163.com)

 * 修改日期	: $Date: 2006-10-27$

 * 当前版本	: $Revision: $

 * 功能描述 : 系统锁、适用于Linux/Windows

 * 修改记录 : 

 *            $Log: $

 **/

#if !defined(AFX_YSLOCK_H__3082C864_723C_4BE6_AB17_2CFE75460894__INCLUDED_)
#define AFX_YSLOCK_H__3082C864_723C_4BE6_AB17_2CFE75460894__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifdef WIN32
	#include <string.h>
	#include <time.h>		/* timespec{} for pselect() */
	#include <stdio.h>
	#include <winsock2.h> 
#else
	#include "localmutex.h"
#endif
#include <string>

#define MUTEX_TIMEOUT 60000	//信号锁的超时毫秒数

class YsLock  
{
public:


	YsLock()
	{
		#ifdef WIN32
			m_hMutex = NULL;
			char name[128];
			memset(name,0,128);
			sprintf(name,"MUTEX_%ld_%d",(long)this,rand()%10000);
			m_hMutex = CreateMutex(NULL, FALSE, name);
		#endif
	}

	~YsLock()
	{
		#ifdef WIN32
			if(m_hMutex != NULL)
			{
				ReleaseMutex(m_hMutex);
				CloseHandle(m_hMutex);
			}
		#endif
	}

	void lock()
	{
		#ifdef WIN32
			if(m_hMutex == NULL)
			{
				printf("YsLock object is not instanced!\n");
				return;
			}
			int ret = WaitForSingleObject(m_hMutex, MUTEX_TIMEOUT);
			if( ret != 0 )
			{
				printf("Wait for mutex timeout!!!!\n");
			}
		#else
			m_Mutex.lock();
		#endif
	}

	void unlock()
	{
		#ifdef WIN32
			if(m_hMutex == NULL)
			{
				printf("YsLock object is not instanced!\n");
				return;
			}
			ReleaseMutex( m_hMutex );
		#else
			m_Mutex.unlock();
		#endif
	}

private:

#ifdef WIN32 
	HANDLE m_hMutex;//互拆体
#else
	CLocalMutex m_Mutex;
#endif

};

#endif // !defined(AFX_YSLOCK_H__3082C864_723C_4BE6_AB17_2CFE75460894__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -