thread.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // thread.hpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_THREAD_HPP
  11. #define ASIO_THREAD_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/noncopyable.hpp"
  17. #include "asio/detail/thread.hpp"
  18. namespace asio {
  19. /// A simple abstraction for starting threads.
  20. /**
  21. * The asio::thread class implements the smallest possible subset of the
  22. * functionality of boost::thread. It is intended to be used only for starting
  23. * a thread and waiting for it to exit. If more extensive threading
  24. * capabilities are required, you are strongly advised to use something else.
  25. *
  26. * @par Thread Safety
  27. * @e Distinct @e objects: Safe.@n
  28. * @e Shared @e objects: Unsafe.
  29. *
  30. * @par Example
  31. * A typical use of asio::thread would be to launch a thread to run an
  32. * io_service's event processing loop:
  33. *
  34. * @par
  35. * @code asio::io_service io_service;
  36. * // ...
  37. * asio::thread t(boost::bind(&asio::io_service::run, &io_service));
  38. * // ...
  39. * t.join(); @endcode
  40. */
  41. class thread
  42. : private noncopyable
  43. {
  44. public:
  45. /// Start a new thread that executes the supplied function.
  46. /**
  47. * This constructor creates a new thread that will execute the given function
  48. * or function object.
  49. *
  50. * @param f The function or function object to be run in the thread. The
  51. * function signature must be: @code void f(); @endcode
  52. */
  53. template <typename Function>
  54. explicit thread(Function f)
  55. : impl_(f)
  56. {
  57. }
  58. /// Destructor.
  59. ~thread()
  60. {
  61. }
  62. /// Wait for the thread to exit.
  63. /**
  64. * This function will block until the thread has exited.
  65. *
  66. * If this function is not called before the thread object is destroyed, the
  67. * thread itself will continue to run until completion. You will, however,
  68. * no longer have the ability to wait for it to exit.
  69. */
  70. void join()
  71. {
  72. impl_.join();
  73. }
  74. private:
  75. detail::thread impl_;
  76. };
  77. } // namespace asio
  78. #include "asio/detail/pop_options.hpp"
  79. #endif // ASIO_THREAD_HPP