summaryrefslogtreecommitdiff
path: root/examples/thread/thread.cc
blob: f535254d5c602331c44da8ba172c1fdb85de3f4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <memory>
#if defined (_MSC_VER) && (_MSC_VER < 1900)
/* For using noexcept on Visual Studio 2013 */
#include <glibmmconfig.h>
#endif
#include <glibmm/random.h>
#include <glibmm/timer.h>
#include <glibmm/init.h>

namespace
{

class MessageQueue
{
public:
  MessageQueue();
  ~MessageQueue();

  void producer();
  void consumer();

private:
  std::mutex mutex_;
  std::condition_variable cond_push_;
  std::condition_variable cond_pop_;
  std::queue<int> queue_;
};


MessageQueue::MessageQueue()
{}

MessageQueue::~MessageQueue()
{}

void MessageQueue::producer()
{
  Glib::Rand rand (1234);

  for(auto i = 0; i < 200; ++i)
  {
    {
      std::unique_lock<std::mutex> lock (mutex_);

      cond_pop_.wait(lock,
        [this] () -> bool
        {
          return queue_.size() < 64;
        });

      queue_.push(i);
      std::cout << '*';
      std::cout.flush();

      //We unlock before notifying, because that is what the documentation suggests:
      //http://en.cppreference.com/w/cpp/thread/condition_variable
      lock.unlock();
      cond_push_.notify_one();
    }

    if(rand.get_bool())
      continue;

    Glib::usleep(rand.get_int_range(0, 100000));
  }
}

void MessageQueue::consumer()
{
  Glib::Rand rand (4567);

  for(;;)
  {
    {
      std::unique_lock<std::mutex> lock (mutex_);

      cond_push_.wait(lock,
        [this] () -> bool
        {
          return !queue_.empty();
        });

      const int i = queue_.front();
      queue_.pop();
      std::cout << "\x08 \x08";
      std::cout.flush();

      //We unlock before notifying, because that is what the documentation suggests:
      //http://en.cppreference.com/w/cpp/thread/condition_variable
      lock.unlock();
      cond_pop_.notify_one();

      if(i >= 199)
        break;
    }

    if(rand.get_bool())
      continue;

    Glib::usleep(rand.get_int_range(10000, 200000));
  }
}

}


int main(int, char**)
{
  Glib::init();

  MessageQueue queue;

  //TODO: Use std::make_unique() when we use C++14:
  const auto producer = std::unique_ptr<std::thread>(
    new std::thread(&MessageQueue::producer, &queue));

  const auto consumer = std::unique_ptr<std::thread>(
    new std::thread(&MessageQueue::consumer, &queue));

  producer->join();
  consumer->join();

  std::cout << std::endl;

  return 0;
}