Subversion Repositories HomeAutomation

Rev

Rev 1317 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * Thread.cpp
  3.  *
  4.  *  Created on: Apr 26, 2009
  5.  *      Author: Mattias Runge
  6.  */
  7.  
  8. #include "Thread.h"
  9.  
  10. using namespace std;
  11.  
  12. namespace atom {
  13. namespace thread {
  14.  
  15. Thread::Thread()
  16. {
  17.     this->is_running_ = false;
  18. }
  19.  
  20. Thread::~Thread()
  21. {
  22.     //cout << "Thread destroy" << endl;
  23.     this->Stop();
  24. }
  25.  
  26. void Thread::Start()
  27. {
  28.     if (!this->is_running_)
  29.     {
  30.         boost::thread localThread(boost::bind(&Thread::RunBase, this));
  31.         this->thread_ = localThread.move();
  32.         this->is_running_ = true;
  33.     }
  34. }
  35.  
  36. void Thread::Stop()
  37. {
  38.     this->thread_.interrupt();
  39.     this->thread_.join();
  40.     this->is_running_ = false;
  41. }
  42.  
  43. boost::thread::id Thread::GetId()
  44. {
  45.     return this->thread_.get_id();
  46. }
  47.  
  48. bool Thread::IsRunning()
  49. {
  50.     return this->is_running_;
  51. }
  52.  
  53. void Thread::RunBase()
  54. {
  55.     this->is_running_ = true;
  56.     this->Run();
  57.     this->is_running_ = false;
  58. }
  59.  
  60. } // namespace thread
  61. } // namespace atom
  62.