MiniBachelor
Buffer.hpp
gehe zur Dokumentation dieser Datei
1 #ifndef _BUFFER_HPP
2 #define _BUFFER_HPP 1
3 #include <mutex>
4 #include <deque>
5 #include "Semaphore.hpp"
6 using namespace std;
7 
20 template<typename ELEMENTTYPE>
21 class Buffer
22 {
23 private:
24  deque<ELEMENTTYPE> _values;
26  mutex _mu;
27 
28 public:
30  {
31  _filled = new Semaphore(0);
32  }
33 
34  virtual ~Buffer()
35  {
36  delete _filled;
37  }
38 
39  ELEMENTTYPE pop()
40  {
41  _filled->P(); // blockieren, wenn leer
42  _mu.lock(); // start: kritischer Abschnitt -------.
43  ELEMENTTYPE _return; // |
44  _return = _values.back(); // |
45  _values.pop_back(); // |
46  _mu.unlock(); // ende: kritischer Abschnitt ------'
47  return _return;
48  }
49 
50  void push(ELEMENTTYPE ele)
51  {
52  _mu.lock(); // start: kritischer Abschnitt ------.
53  _values.push_back(ele); // |
54  _mu.unlock(); // ende: kritischer Abschnitt -----'
55  _filled->V(); // freigeben
56  }
57 
58 };
59 #endif
Buffer()
Definition: Buffer.hpp:29
void push(ELEMENTTYPE ele)
Definition: Buffer.hpp:50
Definition: Semaphore.hpp:6
ELEMENTTYPE pop()
Definition: Buffer.hpp:39
Semaphore * _filled
Definition: Buffer.hpp:25
mutex _mu
Definition: Buffer.hpp:26
virtual ~Buffer()
Definition: Buffer.hpp:34
Definition: Buffer.hpp:21
void V()
Definition: Semaphore.hpp:15
void P()
Definition: Semaphore.hpp:20
deque< ELEMENTTYPE > _values
Definition: Buffer.hpp:24