class Shared { public: Shared() : mTestVar (0) {} int GetVar() { return mTestVar; } void SetVar(int Var) { mTestVar = Var; } boost::mutex mMutex; private: int mTestVar; }; struct ThreadOne { ThreadOne(Shared* pShared) : mpShared (pShared) {} void operator ()() { boost::xtime sleeping_time; int t = 0; bool static first = true; while (5 > mpShared->GetVar()) { boost::mutex::scoped_lock lock(mpShared->mMutex); first = false; t = mpShared->GetVar(); std::cout << "Thread one ... t: " << t << std::endl; boost::xtime_get(&sleeping_time, boost::TIME_UTC); sleeping_time.sec += 3; boost::thread::sleep(sleeping_time); } std::cout << "bigger than 5" << std::endl; } Shared* mpShared; }; struct ThreadTwo { ThreadTwo(Shared* pShared) : mpShared (pShared) {} void operator ()() { int t = 0; boost::xtime sleeping_time; boost::xtime waiting_time; boost::xtime current_time; boost::xtime_get(¤t_time, boost::TIME_UTC); while (current_time.sec + 20 > waiting_time.sec ) { boost::mutex::scoped_lock lock(mpShared->mMutex); mpShared->SetVar(++t); std::cout << "Thread two ... t: " << t << std::endl; boost::xtime_get(&sleeping_time, boost::TIME_UTC); sleeping_time.sec += 1; boost::thread::sleep(sleeping_time); boost::xtime_get(&waiting_time, boost::TIME_UTC); } std::cout << "finished" << std::endl; } Shared* mpShared; }; int main() { Shared* p_shared = new Shared(); ThreadOne thread_one(p_shared); ThreadTwo thread_two(p_shared); boost::thread test_first(thread_one); boost::thread test_second(thread_two); test_first.join(); test_second.join(); }