test_5542_1.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2010 Vicente Botet
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #define BOOST_THREAD_VERSION 2
  6. #include <iostream>
  7. #include <boost/thread/thread_only.hpp>
  8. class Worker
  9. {
  10. public:
  11. Worker()
  12. {
  13. // the thread is not-a-thread until we call start()
  14. }
  15. void start(int N)
  16. {
  17. // std::cout << "start\n";
  18. m_Thread = boost::thread(&Worker::processQueue, this, N);
  19. // std::cout << "started\n";
  20. }
  21. void join()
  22. {
  23. m_Thread.join();
  24. }
  25. void processQueue(unsigned N)
  26. {
  27. unsigned ms = N * 1000;
  28. boost::posix_time::milliseconds workTime(ms);
  29. // std::cout << "Worker: started, will work for "
  30. // << ms << "ms"
  31. // << std::endl;
  32. // We're busy, honest!
  33. boost::this_thread::sleep(workTime);
  34. // std::cout << "Worker: completed" << std::endl;
  35. }
  36. private:
  37. boost::thread m_Thread;
  38. };
  39. int main()
  40. {
  41. // std::cout << "main: startup" << std::endl;
  42. Worker worker;
  43. // std::cout << "main: create worker" << std::endl;
  44. worker.start(3);
  45. // std::cout << "main: waiting for thread" << std::endl;
  46. worker.join();
  47. // std::cout << "main: done" << std::endl;
  48. return 0;
  49. }