thread.cpp 821 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright (C) 2001-2003
  2. // William E. Kempf
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #define BOOST_THREAD_VERSION 2
  7. #include <boost/thread/thread_only.hpp>
  8. #include <boost/thread/xtime.hpp>
  9. #include <iostream>
  10. struct thread_alarm
  11. {
  12. thread_alarm(int secs) : m_secs(secs) { }
  13. void operator()()
  14. {
  15. boost::xtime xt;
  16. boost::xtime_get(&xt, boost::TIME_UTC_);
  17. xt.sec += m_secs;
  18. boost::thread::sleep(xt);
  19. std::cout << "alarm sounded..." << std::endl;
  20. }
  21. int m_secs;
  22. };
  23. int main()
  24. {
  25. int secs = 5;
  26. std::cout << "setting alarm for 5 seconds..." << std::endl;
  27. thread_alarm alarm(secs);
  28. boost::thread thrd(alarm);
  29. thrd.join();
  30. }