设为首页 加入收藏

TOP

多线程互斥--mutex(二)
2015-07-20 17:31:57 来源: 作者: 【 】 浏览:13
Tags:线程 --mutex
turn * - OST_TRUE if the mutex was successfully locked. * - OST_FALSE otherwise. */ OSTBool TryLock(long millisecondes); private: mutable OST_MUTEX_SECTION m_mutex; }; /** * @class AutoLock * @brief Using the AutoLock class is the preferred way to automatically * lock and unlock a mutex. */ class AutoLock { public: AutoLock(const OSTMutex& mutex, OSTBool autolocked = OST_TRUE) : m_mutex(mutex), m_locked(autolocked) { if(autolocked) { m_mutex.Lock(); m_locked = autolocked; } }; ~AutoLock() { if(m_locked) { m_mutex.Unlock(); } }; private: AutoLock(const AutoLock&); AutoLock& operator = (const AutoLock&); private: const OSTMutex& m_mutex; OSTBool m_locked; }; /** * @class AutoUnLock * @brief Using the AutoUnLock class is the preferred way to automatically * lock and unlock a mutex. */ class AutoUnLock { public: AutoUnLock(const OSTMutex& mutex, OSTBool unlocked = OST_TRUE) : m_mutex(mutex), m_unlocked(unlocked) { if(m_unlocked) { m_mutex.Unlock(); } } ~AutoUnLock() { m_mutex.Lock(); } private: AutoUnLock(const AutoUnLock&); AutoUnLock& operator = (const AutoUnLock&); private: const OSTMutex& m_mutex; OSTBool m_unlocked; }; #define LOCK(mutex) AutoLock locker(mutex) #define UNLOCK(mutex) AutoUnLock locker(mutex) OST_NAMESPACE_END #endif//OST_CORE_OSTMUTEX_H


OSTMutex_POSIX.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)
{
	pthread_mutexattr_t attr;
	
	if( 0 != pthread_mutexattr_init(&attr) || 0 != pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
		return;

	if (0 != pthread_mutex_init(&m_mutex, &attr))
	{
		pthread_mutexattr_destroy(&attr);
		throw SystemExc("can
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Leetcode:integer_to_roman 下一篇HDU 1124 Factorial (??)

评论

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

·“我用Java 8”已成 (2025-12-26 11:19:54)
·下载 IntelliJ IDEA (2025-12-26 11:19:52)
·Java是什么?(通俗 (2025-12-26 11:19:49)
·雾里看花:真正意义 (2025-12-26 10:54:36)
·C++——模板(超详细 (2025-12-26 10:54:34)