设为首页 加入收藏

TOP

多线程互斥--mutex(三)
2015-07-20 17:31:57 来源: 作者: 【 】 浏览:10
Tags:线程 --mutex
not create mutex"); } pthread_mutexattr_destroy(&attr); } OSTMutex::~OSTMutex(void) { pthread_mutex_destroy(&m_mutex); } void OSTMutex::Lock() const { try { pthread_mutex_lock(&m_mutex); } catch (...) { throw SystemExc("Cannot lock mutex"); } } void OSTMutex::Unlock() const { pthread_mutex_unlock(&m_mutex); } OSTBool OSTMutex::TryLock() { OSTInt32 rc = pthread_mutex_trylock(&m_mutex); if (0 == rc) { return OST_TRUE; } else if (rc == EBUSY) { return OST_FALSE; } else { throw SystemExc("Cannot lock mutex"); } } OSTBool OSTMutex::TryLock(long millisecondes) { return OST_TRUE; } OST_NAMESPACE_END
OSTMutex_Win32.cpp

/*****************************************************************************
*  OpenST Basic tool library                                                 *
*  Copyright (C) 2014 Henry.Wen  renhuabest@sina.com   .                     *
*                                                                            *
*  This file is part of OST.                                                 *
*                                                                            *
*  This program is free software; you can redistribute it and/or modify      *
*  it under the terms of the GNU General Public License version 3 as         *
*  published by the Free Software Foundation.                                *
*                                                                            *
*  You should have received a copy of the GNU General Public License         *
*  along with OST. If not, see 
  .               *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
*                                                                            *
*  Author  : Henry.Wen                                                       *
*  E-Mail  : renhuabest@sina.com                                             *
*  License : GNU General Public License (GPL)                                *
*  source code availability:https://github.com/henrywen2011/OST              *
*                                                                            *
*----------------------------------------------------------------------------*
*  Remark         : Description						     *
*----------------------------------------------------------------------------*
*  Change History :                                                          *
*  Date       | Version | Author        | Description                        *
*----------------------------------------------------------------------------*
*  2014/01/24 | 1.0.0.1 | Henry.Wen     | Create file                        *
*----------------------------------------------------------------------------*
*                                                                            *
*****************************************************************************/
#include "OSTBaseExc.h"
#include "OSTMutex.h"

OST_NAMESPACE_BEGIN

OSTMutex::OSTMutex(void)
{
	InitializeCriticalSectionAndSpinCount(&m_mutex, 4000);
}

OSTMutex::~OSTMutex(void)
{
	DeleteCriticalSection(&m_mutex);
}

void OSTMutex::Lock() const
{
	try
	{
		EnterCriticalSection(&m_mutex);
	}
	catch (...)
	{
		throw SystemExc("Cannot lock mutex");
	}
}

void OSTMutex::Unlock() const
{
	LeaveCriticalSection(&m_mutex);
}

OSTBool OSTMutex::TryLock()
{
	try
	{
		return (TryEnterCriticalSection(&m_mutex) != 0 ? OST_TRUE : OST_FALSE);
	}
	catch(...)
	{
	}
	throw SystemExc("Cannot lock mutex");
}

OSTBool OSTMutex::TryLock(long millisecondes)
{
	return OST_TRUE;
}

OST_NAMESPACE_END



首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Leetcode:integer_to_roman 下一篇HDU 1124 Factorial (??)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++ Lambda表达式保 (2025-12-26 05:49:45)
·C++ Lambda表达式的 (2025-12-26 05:49:42)
·深入浅出 C++ Lambda (2025-12-26 05:49:40)
·C语言指针从入门到基 (2025-12-26 05:21:36)
·【C语言指针初阶】C (2025-12-26 05:21:33)