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 <set>
00022 #include <string>
00023 #include <iostream>
00024 #include <pthread.h>
00025 #include <boost/utility.hpp>
00026
00027 namespace Raul {
00028
00029
00039 class Thread : boost::noncopyable
00040 {
00041 public:
00042 virtual ~Thread() {
00043 stop();
00044 }
00045
00046 static Thread* create(const std::string& name="")
00047 { return new Thread(name); }
00048
00050 static Thread* create_for_this_thread(const std::string& name="")
00051 { return new Thread(pthread_self(), name); }
00052
00053 static Thread& get();
00054
00055 virtual void start();
00056 virtual void stop();
00057
00058 void set_scheduling(int policy, unsigned int priority);
00059
00060 const std::string& name() const { return _name; }
00061 void set_name(const std::string& name) { _name = name; }
00062
00063 bool is_context(unsigned context) const { return _contexts.find(context) != _contexts.end(); }
00064 void set_context(unsigned context) { _contexts.insert(context); }
00065
00066 protected:
00067 Thread(const std::string& name="");
00068 Thread(pthread_t thread, const std::string& name="");
00069
00078 virtual void _run() {}
00079
00080 bool _exit_flag;
00081
00082 private:
00083 static void* _static_run(void* me);
00084
00086 static void thread_key_alloc() {
00087 pthread_key_create(&_thread_key, NULL);
00088 }
00089
00090
00091 static pthread_key_t _thread_key;
00092
00093
00094 static pthread_once_t _thread_key_once;
00095
00096 std::set<unsigned> _contexts;
00097 std::string _name;
00098 bool _pthread_exists;
00099 bool _own_thread;
00100 pthread_t _pthread;
00101 };
00102
00103
00104 }
00105
00106 #endif // RAUL_THREAD_HPP