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

📄 thread_mutex.h

📁 多线程库
💻 H
字号:
#ifndef __THREADS_MUTEX_H#define __THREADS_MUTEX_H#include <thread_attributes.h>#include <thread_spinlock.h>class wait_queue;/** * Mutex, is a class for mutual exclusion of parallel * processes.  That need to access the same memory, while * keeping exclusive access. * * @short Class for mutual exclusion of parallel processes. * @author Orn Hansen <oe.hansen@gamma.telenordia.se> */class mutex { public:  enum mutex_kind {     fast,     recursive,     errorcheck   }; private:  static char *m_project;  int m_id;  wait_queue *m_waiting;  attributes::scope m_scope;  struct storage {    int m_magic;    spinlock m_spinlock;    int m_count;    int m_owner;    mutex_kind m_kind;  } *_m;  void release();  int acquire();  void init(attributes::scope); public:  mutex(attributes::scope);  mutex();  ~mutex();  /**   * Each mutex variable, can exist within a single process   * hierarchy or be viewed system wide.  The range of the   * scope is:   *   * <pre>   * process_shared     - interprocess   * process_private    - only within this process and its threads.   * </pre>   *   * @return The scope of this mutex.   */  attributes::scope scope();  /**   * Try and lock the mutex, and return immediately to the   * caller with the result.   *   * @return EBUSY if the mutex is busy, EINVAL if it is an unknown kind and 0 on success.   */  int trylock();  /**   * Lock the mutex, and wait for ut if it is already   * locked by another.  This will suspend the calling process   * until such time the lock is successful, or an error occurs.   *   * @return 0 on success, EINVAL if the mutex is an unknown kind.   */  int lock();  /**   * Unlock a previously locked mutex, and restart any threads that   * are waiting on this mutex.   *   * @return 0 on success, EBUSY if it is locked by another process, and EINVAL if the mutex is an unknown kind.   */  int unlock();  /**   * The following methods allow the specification of different mutex   * variable kinds.  The differences are in how they are treated, not   * in how they function. See the next method, for a discussion on   * mutex kinds.   *   * @return fast for a fast mutex, and recursive for a mutex that can be locked several times by it's owner.   */  int kind();  /**   * This method, sets the mutex kind and returns the old value.  The   * valid mutex kinds are:   *   * <pre>   * mutex_kind::fast       - can only be locked once, by any process.   * mutex_kind::recursive  - can be locked multiple times, by the owner. of   *                          the original lock.  it must be unlocked equally   *                          many times.   * mutex_kind::errorcheck - this is a fast mutex, but with simple checks   *                          that will enable the user to detect possible   *                          dedlocks.   * </pre>   *   * @param kind The mutex kind, see above.   * @return the previous kind, prior to setting.   */  int kind(mutex_kind);  /**   * The shared memory scheme, wants a single name to identify the   * overall program.  It is possible, and perhaps desired that   * part cond/mutex/semaphore have their own name identification   * tree.  This is possible, by stating that it should be branch   * of the main file name, with a give extension.   *   * <pre>   * main()   * {   *   mutex *ml;   *   *   pthread::set_project( "my_project" );   *   mutex::project_par( "mutex" );   *   ml = new mutex(attributes::process_shared);   *   ...   * }   * </pre>   *   * This will set the project to my_project, and all mutexes   * will be derived from my_project_mutex.   *   * @param part A C string containing the mutex part name.   */  static void project_part(const char *);};#endif /* THREADS MUTEX */

⌨️ 快捷键说明

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