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

📄 semaphore.h

📁 C++ SOCKET 类
💻 H
字号:
/*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   *  *                                                                         * ***************************************************************************//** * Copyright (C) 1999 * * This file is a part of the C++ threads library. * Implments semaphores for threaded environment as well as shared * process environment. * * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se> */#ifndef __THREADS_SEMAPHORE_H#define __THREADS_SEMAPHORE_H#include <threads/attributes.h>#include <threads/spinlock.h>#include <string>namespace cpp_threads {  class WaitQueue;  /**   * A semaphore, it functions in much a similar way as a mutex   * does, but with some exceptions.   *   * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se>   * @short Implements a semaphore, for thread synchronisation.   */  class Semaphore {  private:    /**     * Perform the initialization of the semaphore data area,     * in either shared or private space.     *     * @param s The scope of the semaphore.     * @param c The initial count.     */    void init(attributes::scope,int);  protected:    static std::string _project;    int                _id;    WaitQueue*         _waiting;    scope_t            _scope;    struct storage {      int      s_magic;      spinlock s_sync;      int      s_count;    } *_s;  public:    Semaphore(scope_t,int c=0);    Semaphore(int c=0);    ~Semaphore();    /**     * Each semaphore, can have one of the following scopes.     *     * <pre>     * process_shared_e     - interprocess     * process_private_e    - only within this process and its threads.     * </pre>     *     * @return The scope of this semaphore.     */    scope_t scope();    /**     * Increment the semaphore count, the top most blocking thread on     * the waiting list is restarted.     *     * @return The updated value, of the semaphore count.     */    int post();    /**     * Block the calling process, until the semaphore count becomes     * greater than 1, then atomically decrement it.     *      * @param p Priority of process, on the waiting queue.     * @return The current count of the semaphore.     */    int wait(int p=-1);    /**     * Decrement the count, if it is greater than zero.  This is a     * non-blocking call.     *     * @return The count of the semaphore after decrement, or -1 not.     */    int tryWait();    /**     * 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()     * {     *   Semaphore *sv;     *     *   Pthread::setProject( "my_project" );     *   Semaphore::projectPart( "sem" );     *   sv = new Semaphore(attributes::process_shared_e);     *   ...     * }     * </pre>     *     * This will set the project to my_project, and all semaphores     * will be derived from my_project_sem.     *     * @param part A C string containing the semaphore part name.     */    static void projectPart(const std::string&);  };}; // namespace#endif /* __THREADS_SEMAPHORE_H */

⌨️ 快捷键说明

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