threads.qbk 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  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. /]
  7. [section:threads Threads and Boost.Asio]
  8. [heading Thread Safety]
  9. In general, it is safe to make concurrent use of distinct objects, but unsafe
  10. to make concurrent use of a single object. However, types such as `io_context`
  11. provide a stronger guarantee that it is safe to use a single object
  12. concurrently.
  13. [heading Thread Pools]
  14. Multiple threads may call `io_context::run()` to set up a pool of threads from
  15. which completion handlers may be invoked. This approach may also be used with
  16. `post()` as a means to perform arbitrary computational tasks across a thread
  17. pool.
  18. Note that all threads that have joined an `io_context`'s pool are considered
  19. equivalent, and the `io_context` may distribute work across them in an
  20. arbitrary fashion.
  21. [heading Internal Threads]
  22. The implementation of this library for a particular platform may make use of
  23. one or more internal threads to emulate asynchronicity. As far as possible,
  24. these threads must be invisible to the library user. In particular, the threads:
  25. * must not call the user's code directly; and
  26. * must block all signals.
  27. This approach is complemented by the following guarantee:
  28. * Asynchronous completion handlers will only be called from threads that are
  29. currently calling `io_context::run()`.
  30. Consequently, it is the library user's responsibility to create and manage all
  31. threads to which the notifications will be delivered.
  32. The reasons for this approach include:
  33. * By only calling `io_context::run()` from a single thread, the user's code can
  34. avoid the development complexity associated with synchronisation. For
  35. example, a library user can implement scalable servers that are
  36. single-threaded (from the user's point of view).
  37. * A library user may need to perform initialisation in a thread shortly after
  38. the thread starts and before any other application code is executed. For
  39. example, users of Microsoft's COM must call `CoInitializeEx` before any other
  40. COM operations can be called from that thread.
  41. * The library interface is decoupled from interfaces for thread creation and
  42. management, and permits implementations on platforms where threads are not
  43. available.
  44. [heading See Also]
  45. [link boost_asio.reference.io_context io_context],
  46. [link boost_asio.reference.post post].
  47. [endsect]