summaryrefslogtreecommitdiff
path: root/examples/thread/dispatcher.cc
blob: cfbd7c016f37749f3a2ad7de3aab3b4df0d4731d (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Glib::Dispatcher example -- cross thread signalling
 * by Daniel Elstner  <daniel.elstner@gmx.net>
 *
 * modified to only use glibmm
 * by J. Abelardo Gutierrez <jabelardo@cantv.net>
 *
 * Copyright (c) 2002-2003  Free Software Foundation
 */

#include <sigc++/class_slot.h>
#include <glibmm.h>

#include <iostream>
#include <algorithm>
#include <functional>
#include <list>

namespace
{
Glib::RefPtr<Glib::MainLoop> main_loop;

class ThreadProgress : public sigc::trackable
{
public:
  ThreadProgress(int id, Glib::Mutex& mtx);
  virtual ~ThreadProgress();

  void launch();

  typedef sigc::signal<void, ThreadProgress*> type_signal_finished;
  type_signal_finished& signal_finished();
  int id() const;

private:
  unsigned int        progress_;
  Glib::Dispatcher    signal_increment_;
  type_signal_finished signal_finished_;
  int                 id_;
  Glib::Mutex&        cout_mutex_;

  void progress_increment();
  void thread_function();
};

//TODO: Rename to avoid confusion with Glib::Dispatcher.
class Dispatcher : public sigc::trackable
{
public:
  Dispatcher();
  virtual ~Dispatcher();

  void launch_threads();

private:
  std::list<ThreadProgress*>  progress_list_;
  Glib::Mutex                 cout_mutex_;

  void on_progress_finished(ThreadProgress* progress);
};


ThreadProgress::ThreadProgress(int id, Glib::Mutex& mtx)
: 
  progress_ (0), id_ (id), cout_mutex_ (mtx)
{
  // Connect to the cross-thread signal.
  signal_increment_.connect(sigc::mem_fun(*this, &ThreadProgress::progress_increment));
}

ThreadProgress::~ThreadProgress()
{}

void ThreadProgress::launch()
{
  // Create a non-joinable thread -- it's deleted automatically on thread exit.
  Glib::Thread::create(sigc::mem_fun(*this, &ThreadProgress::thread_function), false);
}

ThreadProgress::type_signal_finished& ThreadProgress::signal_finished()
{
  return signal_finished_;
}

int ThreadProgress::id() const
{
  return id_;
}

void ThreadProgress::progress_increment()
{
  // Use an integer because floating point arithmetic is inaccurate --
  // we want to finish *exactly* after the 100th increment.
  ++progress_;

  cout_mutex_.lock();
  std::cout << "Thread " << id_ << ": " << progress_ << " %" << std::endl;
  cout_mutex_.unlock();

  if(progress_ >= 100)
    signal_finished().emit(this);
}

void ThreadProgress::thread_function()
{
  Glib::Rand rand;
  int usecs = 5000;

  for(int i = 0; i < 100; ++i)
  {
    usecs = rand.get_int_range(std::max(0, usecs - 1000 - i), std::min(20000, usecs + 1000 + i));
    Glib::usleep(usecs);

    // Tell the thread to increment the progress value.
    signal_increment_.emit();
  }
}

Dispatcher::Dispatcher()
: 
  cout_mutex_ ()
{
  std::cout << "Thread Dispatcher Example." << std::endl;

  for(int i = 0; i < 5; ++i)
  {
    ThreadProgress *const progress = new ThreadProgress(i, cout_mutex_);
    progress_list_.push_back(progress);

    progress->signal_finished().connect(
        sigc::mem_fun(*this, &Dispatcher::on_progress_finished));
  }
}

Dispatcher::~Dispatcher()
{}

void Dispatcher::launch_threads()
{
  std::for_each(
      progress_list_.begin(), progress_list_.end(),
      std::mem_fun(&ThreadProgress::launch));
}

void Dispatcher::on_progress_finished(ThreadProgress* progress)
{
  cout_mutex_.lock();
  std::cout << "Thread " << progress->id() << ": finished." << std::endl;
  cout_mutex_.unlock();

  progress_list_.remove(progress);

  if(progress_list_.empty())
    main_loop->quit();
}

} // anonymous namespace


int main(int argc, char** argv)
{
  Glib::thread_init();
  main_loop = Glib::MainLoop::create();

  Dispatcher dispatcher;

  // Install a one-shot idle handler to launch the threads
  Glib::signal_idle().connect(
      sigc::bind_return(sigc::mem_fun(dispatcher, &Dispatcher::launch_threads), false));

  main_loop->run();

  return 0;
}