00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef RAUL_THREAD_HPP
00019 #define RAUL_THREAD_HPP
00020
00021 #include <iostream>
00022 #include <set>
00023 #include <string>
00024
00025 #include <pthread.h>
00026
00027 #include <boost/utility.hpp>
00028
00029 namespace Raul {
00030
00031
00041 class Thread : boost::noncopyable
00042 {
00043 public:
00044 virtual ~Thread() {
00045 stop();
00046 }
00047
00048 static Thread* create(const std::string& name="")
00049 { return new Thread(name); }
00050
00052 static Thread* create_for_this_thread(const std::string& name="")
00053 { return new Thread(pthread_self(), name); }
00054
00055 static Thread& get();
00056
00057 virtual void start();
00058 virtual void stop();
00059
00060 virtual void join();
00061
00062 void set_scheduling(int policy, unsigned int priority);
00063
00064 const std::string& name() const { return _name; }
00065 void set_name(const std::string& name) { _name = name; }
00066
00067 bool is_context(unsigned context) const { return _contexts.find(context) != _contexts.end(); }
00068 void set_context(unsigned context) { _contexts.insert(context); }
00069
00070 protected:
00071 explicit Thread(const std::string& name="");
00072 Thread(pthread_t thread, const std::string& name="");
00073
00082 virtual void _run() {}
00083
00084 bool _exit_flag;
00085
00086 private:
00087 static void* _static_run(void* me);
00088
00090 static void thread_key_alloc() {
00091 pthread_key_create(&_thread_key, NULL);
00092 }
00093
00094
00095 static pthread_key_t _thread_key;
00096
00097
00098 static pthread_once_t _thread_key_once;
00099
00100 std::set<unsigned> _contexts;
00101 std::string _name;
00102 bool _pthread_exists;
00103 bool _own_thread;
00104 pthread_t _pthread;
00105 };
00106
00107
00108 }
00109
00110 #endif // RAUL_THREAD_HPP