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

📄 mutex.h

📁 遗传算法的一个库
💻 H
字号:
#ifndef __GradSoft_Mutex_h#define __GradSoft_Mutex_h/* * GradSoft C++  ToolBox Threading Library. * Mutex definitions * (C) GradSoft 2000, 2001 * http://www.gradsoft.kiev.ua * Ukraine, Kiev * $Id: Mutex.h,v 1.30 2002/03/25 21:51:23 rssh Exp $ */#ifndef __GradSoft_ThreadingPostConfig_h#include <GradSoft/ThreadingPostConfig.h>#endif#ifndef __GradSoft_ThreadingExceptions_h#include <GradSoft/ThreadingExceptions.h>#endif#ifndef __GradSoft_ThreadingUtils_h#include <GradSoft/ThreadingUtils.h>#endif#ifdef HAVE_PTHREAD_H#include <pthread.h>#endif#ifdef WIN32#include <windows.h>#endif/** * **/namespace GradSoft {/** * Mutual Exclusive Lock (mutex) **/class Mutex{public:  ///  Mutex()  throw (ThreadingExceptions::Failure);  /// ~Mutex()  throw (ThreadingExceptions::Failure);   /**   * lock mutex   **/  void lock(void)  throw (ThreadingExceptions::Failure);  /**   * try lock mutex   *@return true, if mutex is succesfully locked, false otherwise   **/  bool try_lock(void) throw() ;  /**   * unlock mutex   *@precondition mutex must not be locked   **/  void unlock(void)  throw (ThreadingExceptions::Failure);private:#ifdef HAVE_PTHREAD_H  pthread_mutex_t mutex_;#else# if defined(WIN32)  HANDLE mutex_;  int waiters_count_;  CRITICAL_SECTION waiters_count_lock_;# else#  error "Unknown platform, may be WIN32 or HAVE_PTHREAD_H undefined ?"# endif#endif  Mutex(const Mutex& m);  Mutex& operator=(const Mutex& m);};/** * MutexLocker :  *  lock mutex on creation, unlock on desctruction. * Usage: *<code> * { *  MutexLocker locker(mutex); *  .... *  critical section operations is here *  .... * } *</code> **/class MutexLocker{ Mutex& mutex_;public: /// MutexLocker(Mutex& mutex) throw(ThreadingExceptions::Failure)  :mutex_(mutex) { mutex.lock(); } /// ~MutexLocker() throw(ThreadingExceptions::Failure) { bool flg=std::uncaught_exception();   try { mutex_.unlock(); }   catch(const ThreadingExceptions::Failure& ex)   {if (!flg) throw;}  }};}#endif

⌨️ 快捷键说明

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