RAUL  0.8.0
Thread.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_THREAD_HPP
19 #define RAUL_THREAD_HPP
20 
21 #include <iostream>
22 #include <set>
23 #include <string>
24 
25 #include <pthread.h>
26 
27 #include <boost/utility.hpp>
28 
29 namespace Raul {
30 
31 
41 class Thread : boost::noncopyable
42 {
43 public:
44  virtual ~Thread() {
45  stop();
46  }
47 
48  static Thread* create(const std::string& name="")
49  { return new Thread(name); }
50 
52  static Thread* create_for_this_thread(const std::string& name="")
53  { return new Thread(pthread_self(), name); }
54 
55  static Thread& get();
56 
57  virtual void start();
58  virtual void stop();
59 
60  virtual void join();
61 
62  void set_scheduling(int policy, unsigned int priority);
63 
64  const std::string& name() const { return _name; }
65  void set_name(const std::string& name) { _name = name; }
66 
67  bool is_context(unsigned context) const { return _contexts.find(context) != _contexts.end(); }
68  void set_context(unsigned context) { _contexts.insert(context); }
69 
70 protected:
71  explicit Thread(const std::string& name="");
72  Thread(pthread_t thread, const std::string& name="");
73 
82  virtual void _run() {}
83 
84  bool _exit_flag;
85 
86 private:
87  static void* _static_run(void* me);
88 
90  static void thread_key_alloc() {
91  pthread_key_create(&_thread_key, NULL);
92  }
93 
94  /* Key for the thread-specific buffer */
95  static pthread_key_t _thread_key;
96 
97  /* Once-only initialisation of the key */
98  static pthread_once_t _thread_key_once;
99 
100  std::set<unsigned> _contexts;
101  std::string _name;
102  bool _pthread_exists;
103  bool _own_thread;
104  pthread_t _pthread;
105 };
106 
107 
108 } // namespace Raul
109 
110 #endif // RAUL_THREAD_HPP
virtual void stop()
Stop and terminate the thread.
Definition: Thread.cpp:103
Abstract base class for a thread.
Definition: Thread.hpp:41
static Thread * create_for_this_thread(const std::string &name="")
Must be called from thread.
Definition: Thread.hpp:52
virtual void start()
Launch and start the thread.
Definition: Thread.cpp:87
Definition: Array.hpp:26
virtual void _run()
Thread function to execute.
Definition: Thread.hpp:82