Robotics Library  0.6.2
Thread.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef _RL_UTIL_THREAD_H_
28 #define _RL_UTIL_THREAD_H_
29 
30 #ifdef WIN32
31 #include <process.h>
32 #include <windows.h>
33 #else // WIN32
34 #include <pthread.h>
35 #include <unistd.h>
36 #endif // WIN32
37 
38 #include <cstdlib>
39 
40 namespace rl
41 {
42  namespace util
43  {
44  class Thread
45  {
46  public:
47  explicit Thread() :
48 #ifdef WIN32
49  id(0),
50  thread(NULL)
51 #else // WIN32
52  thread()
53 #endif // WIN32
54  {
55  }
56 
57  virtual ~Thread()
58  {
59 #ifdef WIN32
60  CloseHandle((*this).thread);
61 #endif // WIN32
62  }
63 
64  bool operator==(const Thread& rhs) const
65  {
66 #ifdef WIN32
67  return (*this).id == rhs.id;
68 #else // WIN32
69  return pthread_equal((*this).thread, rhs.thread);
70 #endif // WIN32
71  }
72 
73  bool operator!=(const Thread& rhs) const
74  {
75  return !operator==(rhs);
76  }
77 
78  void join()
79  {
80 #ifdef WIN32
81  WaitForSingleObject((*this).thread, INFINITE);
82 #else // WIN32
83  pthread_join((*this).thread, NULL);
84 #endif // WIN32
85  }
86 
87  virtual void run() = 0;
88 
89  static void sleep(const double& seconds)
90  {
91 #ifdef WIN32
92  Sleep(static_cast< unsigned int >(seconds * 1000.0f));
93 #else // WIN32
94  usleep(static_cast< ::std::size_t >(seconds * 1000.0f * 1000.0f));
95 #endif // WIN32
96  }
97 
98  void start()
99  {
100 #ifdef WIN32
101  (*this).thread = reinterpret_cast< void* >(_beginthreadex(NULL, 0, Thread::start, this, 0, &(*this).id));
102 #else // WIN32
103  pthread_create(&(*this).thread, NULL, Thread::start, this);
104 #endif // WIN32
105  }
106 
107  static void yield()
108  {
109 #ifdef WIN32
110  Sleep(0);
111 #else // WIN32
112  sched_yield();
113 #endif // WIN32
114  }
115 
116  protected:
117 
118  private:
119 #ifdef WIN32
120  static unsigned int __stdcall start(void* arg)
121  {
122  static_cast< Thread* >(arg)->run();
123  return 0;
124  }
125 
126  unsigned int id;
127 
128  void* thread;
129 #else // WIN32
130  static void* start(void* arg)
131  {
132  static_cast< Thread* >(arg)->run();
133  return NULL;
134  }
135 
136  pthread_t thread;
137 #endif // WIN32
138  };
139  }
140 }
141 
142 #endif // _RL_UTIL_THREAD_H_
rl::util::Thread::~Thread
virtual ~Thread()
Definition: Thread.h:57
rl::util::Thread::thread
pthread_t thread
Definition: Thread.h:136
rl::util::Thread::operator==
bool operator==(const Thread &rhs) const
Definition: Thread.h:64
rl::util::Thread::start
static void * start(void *arg)
Definition: Thread.h:130
rl::util::Thread::yield
static void yield()
Definition: Thread.h:107
rl::util::Thread::sleep
static void sleep(const double &seconds)
Definition: Thread.h:89
rl::util::Thread::run
virtual void run()=0
rl::util::Thread
Definition: Thread.h:45
rl::util::Thread::start
void start()
Definition: Thread.h:98
rl::util::Thread::Thread
Thread()
Definition: Thread.h:47
rl::util::Thread::operator!=
bool operator!=(const Thread &rhs) const
Definition: Thread.h:73
rl::util::Thread::join
void join()
Definition: Thread.h:78
rl
Definition: Ati.cpp:35