Robotics Library  0.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 // * Neither the name of the Technische Universitaet Muenchen nor the names of
14 // its contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef _RL_UTIL_THREAD_H_
31 #define _RL_UTIL_THREAD_H_
32 
33 #ifdef WIN32
34 #include <process.h>
35 #include <windows.h>
36 #else // WIN32
37 #include <pthread.h>
38 #include <unistd.h>
39 #endif // WIN32
40 
41 #include <cstdlib>
42 
43 namespace rl
44 {
45  namespace util
46  {
47  class Thread
48  {
49  public:
50  explicit Thread() :
51 #ifdef WIN32
52  id(0),
53  thread(NULL)
54 #else // WIN32
55  thread()
56 #endif // WIN32
57  {
58  }
59 
60  virtual ~Thread()
61  {
62 #ifdef WIN32
63  CloseHandle((*this).thread);
64 #endif // WIN32
65  }
66 
67  bool operator==(const Thread& rhs) const
68  {
69 #ifdef WIN32
70  return (*this).id == rhs.id;
71 #else // WIN32
72  return pthread_equal((*this).thread, rhs.thread);
73 #endif // WIN32
74  }
75 
76  bool operator!=(const Thread& rhs) const
77  {
78  return !operator==(rhs);
79  }
80 
81  void join()
82  {
83 #ifdef WIN32
84  WaitForSingleObject((*this).thread, INFINITE);
85 #else // WIN32
86  pthread_join((*this).thread, NULL);
87 #endif // WIN32
88  }
89 
90  virtual void run() = 0;
91 
92  static void sleep(const double& seconds)
93  {
94 #ifdef WIN32
95  Sleep(static_cast< unsigned int >(seconds * 1000.0f));
96 #else // WIN32
97  usleep(static_cast< ::std::size_t >(seconds * 1000.0f * 1000.0f));
98 #endif // WIN32
99  }
100 
101  void start()
102  {
103 #ifdef WIN32
104  (*this).thread = reinterpret_cast< void* >(_beginthreadex(NULL, 0, Thread::start, this, 0, &(*this).id));
105 #else // WIN32
106  pthread_create(&(*this).thread, NULL, Thread::start, this);
107 #endif // WIN32
108  }
109 
110  static void yield()
111  {
112 #ifdef WIN32
113  Sleep(0);
114 #else // WIN32
115  sched_yield();
116 #endif // WIN32
117  }
118 
119  protected:
120 
121  private:
122 #ifdef WIN32
123  static unsigned int __stdcall start(void* arg)
124  {
125  static_cast< Thread* >(arg)->run();
126  return 0;
127  }
128 
129  unsigned int id;
130 
131  void* thread;
132 #else // WIN32
133  static void* start(void* arg)
134  {
135  static_cast< Thread* >(arg)->run();
136  return NULL;
137  }
138 
139  pthread_t thread;
140 #endif // WIN32
141  };
142  }
143 }
144 
145 #endif // _RL_UTIL_THREAD_H_