OpenWalnut  1.4.0
WConditionOneShot.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WCONDITIONONESHOT_H
26 #define WCONDITIONONESHOT_H
27 
28 #ifndef Q_MOC_RUN
29 #include <boost/thread.hpp>
30 #endif
31 
32 #include "WCondition.h"
33 
34 
35 /**
36  * Implements a WCondition, but can be fired only ONCE. This is useful if you want to have a thread waiting for a condition but
37  * you can not assure that the thread already waits when you set the condition. This would cause the thread to wait endlessly
38  * because he does not know that you already fired it. Implementation is simple. The constructor uses a unique lock (write lock)
39  * on a mutex. All waiting threads try to get a read lock which is not possible as long it is write-locked. The notify method
40  * releases the write lock and all waiting threads can continue.
41  */
43 {
44  friend class WConditionOneShot_test;
45 public:
46  /**
47  * Default constructor.
48  */
50 
51  /**
52  * Destructor.
53  */
54  virtual ~WConditionOneShot();
55 
56  /**
57  * Wait for the condition. Sets the calling thread asleep.
58  */
59  virtual void wait() const;
60 
61  /**
62  * Notifies all waiting threads.
63  */
64  virtual void notify();
65 
66 protected:
67  /**
68  * Locked as long the condition was not fired.
69  */
70  boost::unique_lock<boost::shared_mutex> m_lock;
71 
72 private:
73 };
74 
75 #endif // WCONDITIONONESHOT_H
76 
Implements a WCondition, but can be fired only ONCE.
virtual void notify()
Notifies all waiting threads.
boost::unique_lock< boost::shared_mutex > m_lock
Locked as long the condition was not fired.
WConditionOneShot()
Default constructor.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:47
virtual ~WConditionOneShot()
Destructor.
virtual void wait() const
Wait for the condition.