#pragma once #include "stdafx.h" class mMindThread { friend class mMindThreadGroup; public: bool locked; private: boost::thread* _thread; boost::mutex _lock_handler; boost::mutex::scoped_lock* _lock; bool _ready; bool _being_used; unsigned long _id; public: mMindThread() { _ready = false; _being_used = false; locked = false; } ~mMindThread() { if (_ready) delete _thread; } void initialize(void) { //void(*function)(void) _thread = new boost::thread( boost::bind(&mMindThread::function, this) ); _ready = true; } virtual void function(void) {} protected: void lock(void) { _lock = new boost::mutex::scoped_lock(_lock_handler); locked = true; } void unlock(void) { locked = false; delete _lock; } void sleep(long seconds) { boost::system_time amount = boost::get_system_time(); amount += boost::posix_time::seconds(seconds); boost::this_thread::sleep(amount); } public: unsigned long get_id(void) { if (!_being_used) { throw mMindErrorThread("Thread id has not yet been assigned because the thread is not a member of a group!"); } return _id; } void run(void) { if (!_ready) throw mMindErrorThread("Thread has not been initialized!"); _thread->join(); } void stop(void) { _thread->interrupt(); } }; class mMindThreadGroup { private: boost::thread_group* _threads; unsigned long _current_id; public: mMindThreadGroup() { _current_id = 1; _threads = new boost::thread_group(); } ~mMindThreadGroup() { delete _threads; } void add_thread(mMindThread* thread) { if (!thread->_ready) throw mMindErrorThread("Thread has not been initialized!"); if (thread->_being_used) throw mMindErrorThread("Thread is already being used in another group!"); _threads->add_thread(thread->_thread); thread->_being_used = true; thread->_id = _current_id++; } void start_all(void) { _threads->join_all(); } };