设为首页 加入收藏

TOP

多线程互斥--mutex(一)
2015-07-20 17:31:57 来源: 作者: 【 】 浏览:11
Tags:线程 --mutex
多线程之线程同步Mutex (功能与Critial Sections相同,但是属于内核对象,访问速度较慢,可以被不同进程调用)

一 Mutex
互斥对象(mutex)内核对象能够确保线程拥有对单个资源的互斥访问权。实际上互斥对象是因此而得名的。互斥对象包含一个使用数量,一个线程ID和一个递归计数器。
互斥对象的行为特性与关键代码段相同,但是互斥对象属于内核对象,而关键代码段则属于用户方式对象。这意味着互斥对象的运行速度比关键代码段要慢。但是这也意味着不同进程中的多个线程能够访问单个互斥对象,并且这意味着线程在等待访问资源时可以设定一个超时值。
ID用于标识 系统中的哪个线程当前拥有互斥对象,递归计数器用于指明该线程拥有互斥对象的次数。

互斥对象有许多用途,属于最常用的内核对象之一。通常来说,它们用于保护由多个线程访问的内存块。如果多个线程要同时访问内存块,内存块中的数据就可能遭到破坏。互斥对象能够保证访问内存块的任何线程拥有对该内存块的独占访问权,这样就能够保证数据的完整性。

/*****************************************************************************
*  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/26 | 1.0.0.1 | Henry.Wen     | Create file                        *
*----------------------------------------------------------------------------*
*                                                                            *
*****************************************************************************/
#ifndef OST_CORE_OSTMUTEX_H
#define OST_CORE_OSTMUTEX_H

#include "OSTTypes.h"
#include "OSTPlatform.h"
#include "OSTBasicable.h"

#if (OST_PLAFORM == OST_PLATFORM_WIN32)
typedef CRITICAL_SECTION OST_MUTEX_SECTION;
#else
#include 
  
   
typedef pthread_mutex_t  OST_MUTEX_SECTION;
#endif

OST_NAMESPACE_BEGIN
/** 
* @class OSTMutex
* @brief A Mutex (mutual exclusion) is a synchronization mechanism used to control
* access to a shared resourcein a concurrent (multithreaded) scenario.
*/
class OSTMutex : public NonCopyable
{
public:
	OSTMutex(void);
	~OSTMutex(void);

	/**
	* @brief Locks the OSTMutex. Blocks if the OSTMutex is held by another thread.
	*/
	void Lock() const;

	/**
	* @brief Unlocks the mutex so that it can be acquired by other threads.
	*/
	void Unlock() const;

	/**
	* @brief Tries to lock the mutex. 
	* @return 
	*	-
   OST_FALSE if the mutex is already held by another thread
	*	-
   OST_TRUE otherwise.
	*/
	OSTBool TryLock();

	/**
	* @brief Locks the mutex. Blocks up to the given number of milliseconds
	* if the mutex is held by another thread.
	* Performance Note: On most platforms (including Windows), this member function is 
	* implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
	* On POSIX platforms that support pthread_mutex_timedlock(), this is used.
	*
	* @re
首页 上一页 1 2 3 下一页 尾页 1/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)