[C/C++][boost] boost::thread - boost::mutex
2009. 5. 26. 14:52 in Programming/Boost

나중에 boost thread 정리를 좀 제대로 해서 포스팅 해야겠다 -_-;
일단 급한대로..
- #include <boost/thread/thread.hpp>
- #include <boost/thread/mutex.hpp>
- #include <boost/thread/condition.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- #include <boost/bind.hpp>
- class BoostThreadEx
- {
- public:
- BoostThreadEx(void)
- : m_pThread1(NULL)
- , m_pThread2(NULL)
- , m_bThreadContinue(false)
- , m_nThreadCount(0) {}
- virtual BoostThreadEx(void);
- void startThread();
- void threadFunc(const char* pc, int i);
- private:
- boost::thread* m_pThread1;
- boost::thread* m_pThread2;
- boost::mutex m_Mutex;
- // boost::condition m_Condition;
- bool m_bThreadContinue;
- int m_nThreadCount;
- };
- BoostThreadEx::BoostThreadEx(void)
- {
- if(m_pThread1)
- {
- m_pThread1->join();
- delete m_pThread1;
- m_pThread1 = NULL;
- }
- if(m_pThread2)
- {
- m_pThread2->join();
- delete m_pThread2;
- m_pThread2 = NULL;
- }
- }
- void BoostThreadEx::startThread();
- {
- if(!m_pThread1 && !m_pThread2)
- {
- m_pThread1 = new boost::thread(boost::bind(&BoostThreadEx::threadFunc, this, "Thread Test", 1));
- m_pThread2 = new boost::thread(boost::bind(&BoostThreadEx::threadFunc, this, "Thread Test", 2));
- m_bThreadContinue = true;
- }
- }
- void BoostThreadEx::threadFunc(const char* pc, int i)
- {
- while(m_bThreadContinue)
- {
- if(m_bThreadContinue) break;
- // boost::posix_time::millisec const waiting_duration(500);
- // {
- // boost::mutex::scoped_lock lock(m_Mutex);
- // m_Condition.timed_wait(lock, waiting_duration);
- // }
- {
- boost::scoped_lock mlock(m_Mutex);
- printf("%s%d\n", pc, i);
- }
- if(m_nCount++ >= 100) m_bThreadContinue = false;
- }
- }
text 편집기로 대강 짰는데 컴파일 되려나 ㅋㅋㅋ