#include <glibmm.h>
namespace
{
class ThreadProgress
{
public:
  explicit ThreadProgress(int the_id);
  ~ThreadProgress();
  int id() const;
  void join();
  bool unfinished() const;
private:
  enum
  {
    ITERATIONS = 100
  };
  
  
  
  int id_;
  unsigned int progress_;
  void progress_increment();
  void thread_function();
};
{
public:
  Application();
  ~Application();
  void run();
private:
  void launch_threads();
  void on_progress_finished(ThreadProgress* thread_progress);
};
template <class T>
{
public:
  void operator()(T ptr) const { delete ptr; }
};
ThreadProgress::ThreadProgress(int the_id) : thread_(nullptr), id_(the_id), progress_(0)
{
  
  signal_increment_.connect(
sigc::mem_fun(*
this, &ThreadProgress::progress_increment));
}
ThreadProgress::~ThreadProgress()
{
  
  g_return_if_fail(thread_ == nullptr);
}
int
ThreadProgress::id() const
{
  return id_;
}
void
{
  
  thread_ = 
new std::thread([
this]() { thread_function(); });
}
void
ThreadProgress::join()
{
  thread_->join();
  delete thread_;
  thread_ = nullptr;
}
bool
ThreadProgress::unfinished() const
{
  return (progress_ < ITERATIONS);
}
ThreadProgress::signal_finished()
{
  return signal_finished_;
}
void
ThreadProgress::progress_increment()
{
  ++progress_;
  if (progress_ >= ITERATIONS)
    signal_finished_();
}
void
ThreadProgress::thread_function()
{
  for (auto i = 0; i < ITERATIONS; ++i)
  {
    
    signal_increment_();
  }
}
{
  try
  {
    {
      ThreadProgress* const progress = new ThreadProgress(i + 1);
      progress_threads_[i] = progress;
      progress->signal_finished().connect(
    }
  }
  catch (...)
  {
    
    
    std::for_each(progress_threads_.begin(), progress_threads_.end(), DeletePtr<ThreadProgress*>());
     throw;
  }
}
Application::~Application()
{
  std::for_each(progress_threads_.begin(), progress_threads_.end(), DeletePtr<ThreadProgress*>());
 }
void
Application::run()
{
  
  main_loop_->run();
}
void
Application::launch_threads()
{
}
void
Application::on_progress_finished(ThreadProgress* thread_progress)
{
  thread_progress->join();
  
  if (
std::find_if(progress_threads_.begin(), progress_threads_.end(),
         std::mem_fun(&ThreadProgress::unfinished)) == progress_threads_.end())
   {
    main_loop_->quit();
  }
}
} 
int
main(int, char**)
{
  Application application;
  application.run();
  return 0;
}