summaryrefslogtreecommitdiff
path: root/examples/thread/dispatcher.cc
blob: ca74d05db9a0584a35fdeb7f3edfb3af1631ffbe (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * 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 <glibmm.h>

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


namespace
{

class ThreadProgress : public sigc::trackable
{
public:
  explicit ThreadProgress(int id);
  virtual ~ThreadProgress();

  void launch();
  void join();

  sigc::signal<void>& signal_finished();
  int id() const;

  virtual void reference() const { ++ref_count_; }
  virtual void unreference() const { if (!(--ref_count_)) delete this; }

private:
  Glib::Thread*       thread_;
  int                 id_;
  unsigned int        progress_;
  Glib::Dispatcher    signal_increment_;
  sigc::signal<void>  signal_finished_;

  void progress_increment();
  void thread_function();

  mutable int ref_count_;
 
};

class Application : public sigc::trackable
{
public:
  Application();
  virtual ~Application();

  void launch_threads();
  void run();

private:
  Glib::RefPtr<Glib::MainLoop>  main_loop_;
  std::list<ThreadProgress*>    progress_list_;
  std::list<Glib::RefPtr<ThreadProgress> > progress_ref_list_;

  void on_progress_finished(ThreadProgress* thread_progress);
};


ThreadProgress::ThreadProgress(int id)
:
  thread_   (0),
  id_       (id),
  progress_ (0),
  ref_count_(0)
{
  // Increment the reference count
  reference();
  // Connect to the cross-thread signal.
  signal_increment_.connect(sigc::mem_fun(*this, &ThreadProgress::progress_increment));
}

ThreadProgress::~ThreadProgress()
{}

void ThreadProgress::launch()
{
  // Create a joinable thread.
  thread_ = Glib::Thread::create(sigc::mem_fun(*this, &ThreadProgress::thread_function), true);
}

void ThreadProgress::join()
{
  thread_->join();
}

sigc::signal<void>& 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_;

  std::cout << "Thread " << id_ << ": " << progress_ << '%' << std::endl;

  if(progress_ >= 100)
    signal_finished_();
}

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 main thread to increment the progress value.
    signal_increment_();
  }
}

Application::Application()
:
  main_loop_ (Glib::MainLoop::create())
{
  std::cout << "Thread Dispatcher Example." << std::endl;

  for(int i = 1; i <= 5; ++i)
  {
    ThreadProgress* progress=new ThreadProgress(i);
    progress_list_.push_back(progress);
    progress_ref_list_.push_back(Glib::RefPtr<ThreadProgress>(progress));

    progress->signal_finished().connect(
        sigc::bind<1>(sigc::mem_fun(*this, &Application::on_progress_finished), progress));
  }
}

Application::~Application()
{
}

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

void Application::run()
{
  main_loop_->run();
}

void Application::on_progress_finished(ThreadProgress* thread_progress)
{
  {
    progress_list_.remove(thread_progress);
    thread_progress->join();

    std::cout << "Thread " << thread_progress->id() 
              << ": finished." << std::endl;
  }

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

} // anonymous namespace


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

  Application application;

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

  application.run();

  return 0;
}