📄 cond.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 part of the C++ Threadss library. * * Implements condition variables, for threaded environment as well as * shared process environment. * * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se> */#ifndef __THREADS_COND_H#define __THREADS_COND_H#include <threads/mutex.h>#include <threads/attributes.h>#include <threads/spinlock.h>#define __STL_USE_NAMESPACES#include <string>namespace cpp_threads { class WaitQueue; /** * Cond, is a conditional semaphore. A process can wait on a * specific condition... and another, can trigger that condition * through this class. * * One typical example of use of this kind of class, is the * producer consumer example: * * <pre> * * #include <thread.h> * #include <string> * * string produced; * Mutex lock; * Cond signal; * * class producer : public Pthread { * public: * producer() { }; * ~producer() { }; * int thread(void *) { * char buf[80]; * * lock.lock(); * cin.geline(buf,80); * produced = buf; * signal.signal(); * lock.unlock(); * }; * }; * * class consumer : public Pthread { * public: * consumer() { }; * ~consumer() { }; * * int thread(void *) { * lock.lock(); * signal.wait(); * cout << "Production was '" << s.c_str() << "'\n"; * lock.unlock(); * } * } * * main() * { * Pthread *p; * * p = new consumer; * (void)new producer; * p.join(); * } * * </pre> * * Notice the order, in which the two threads are created. The consumer * before the producer. This is because both of these threads start by * locking the mutex lock, and the consumer must be the first one to * actually acquire it. * * @short Class for signalling conditions between processes. * @author Orn Hansen <oe.hansen@gamma.telenordia.se> */ class Cond { private: static std::string _project; int _id; scope_t _scope; WaitQueue* _waiting; public: Cond(scope_t); Cond(); ~Cond(); /** * Each condition variable, 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 condition variable. */ scope_t scope(); /** * This method, waits until a signal has been received * and suspends the calling process during that period. The * thread can be cancelled, while waiting for the signal. * * @param mutex A mutex variable, which is unlocked during the period and locked immediately after. * @return Always returns 0, signifying success. */ int wait(Mutex&,int p=-1); /** * This method will wait for a specified number of micro * seconds for a signal to arrive. It will calculate * the relative time from the number of microseconds * passed, and then call timedwait_rel to do the * waiting. * * @param mutex The mutex to lock. * @param usec Microseconds to wait for a signal before timing out. * @param pri The priority to use on the priority queue. */ int timedWait(Mutex&,long int,int p=-1); /** * This method, acts in a similar manner to the one above, * but it will timeout on the wait when a specific time tick * has been reached. Take a look at @ref #timedwait_rel * for a discussion on possible return values. * @see #timedwait_rel for a discussion on the kinds * of return values that are available. * * @param mutex Mutex variable to lock on signal. * @param timespec The time tick, when a timeout should occur. * @param pri Priority to use on the wait queue. */ int timedWait(Mutex&,const struct timespec *,int p=-1); /** * This method is equivalent to the above, except that the * time specification it accepts is a relative time. Instead * of specifying the absolute time tick to timeout, it specifies * the amount of time to wait, until a timeout should occurr. * This method will return a value, specifying the cause of * return from it. It can be one of: * <pre> * ESRCH - If the calling thread is not known to the system. * EINTR - If an interrupt signal was received and terminated the wait. * ETIMEOUT - If a timeout occurred, prior to receiving a signal. * 0 - On success. * </pre> * * @param mutex The mutex to lock, when signal has been received. * @param timespec A structure with the amount of time to wait for a signal, before timing out. * @param pri Priority to use on the wait queue. */ int timedWaitRel(Mutex&,const struct timespec *,int p=-1); /** * This method, will send a signal to the next process that is * registered in the waiting processes list. That process will * be revived. * * @return Always return 0, signifying success. */ int signal(); /** * This method, will send a signal to every process on the waiting * processes list, reviving them all. All processes will be removed * from the waiting queue. * * @return Always return 0, signifying success. */ int broadcast(); /** * 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() * { * Cond *cv; * * Pthread::setProject( "my_project" ); * Cond::projectPart( "cond" ); * cv = new Cond(attributes::process_shared); * ... * } * </pre> * * This will set the project to my_project, and all condition * variables will be derived from my_project_cond. * * @param part A C string containing the cond part name. */ static void projectPart(const std::string&); };}; // namespace#endif /* THREADS COND */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -