concurrency_hint.qbk 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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:concurrency_hint Concurrency Hints]
  8. The [link boost_asio.reference.io_context.io_context `io_context` constructor]
  9. allows programs to specify a concurrency hint. This is a suggestion to the
  10. `io_context` implementation as to the number of active threads that should be
  11. used for running completion handlers.
  12. When the Windows I/O completion port backend is in use, this value is passed
  13. to [^CreateIoCompletionPort].
  14. When a reactor-based backend is used, the implementation recognises the
  15. following special concurrency hint values:
  16. [table
  17. [[Value][Description]]
  18. [
  19. [`1`]
  20. [
  21. The implementation assumes that the `io_context` will be run from a
  22. single thread, and applies several optimisations based on this
  23. assumption.
  24. For example, when a handler is posted from within another handler, the
  25. new handler is added to a fast thread-local queue (with the consequence
  26. that the new handler is held back until the currently executing handler
  27. finishes).
  28. ]
  29. ]
  30. [
  31. [`BOOST_ASIO_CONCURRENCY_HINT_UNSAFE`]
  32. [
  33. This special concurrency hint disables locking in both the scheduler and
  34. reactor I/O. This hint has the following restrictions:
  35. [mdash] Care must be taken to ensure that all operations on the
  36. `io_context` and any of its associated I/O objects (such as sockets and
  37. timers) occur in only one thread at a time.
  38. [mdash] Asynchronous resolve operations fail with `operation_not_supported`.
  39. [mdash] If a `signal_set` is used with the `io_context`, `signal_set`
  40. objects cannot be used with any other io_context in the program.
  41. ]
  42. ]
  43. [
  44. [`BOOST_ASIO_CONCURRENCY_HINT_UNSAFE_IO`]
  45. [
  46. This special concurrency hint disables locking in the reactor I/O. This
  47. hint has the following restrictions:
  48. [mdash] Care must be taken to ensure that run functions on the
  49. `io_context`, and all operations on the context's associated I/O objects
  50. (such as sockets and timers), occur in only one thread at a time.
  51. ]
  52. ]
  53. [
  54. [`BOOST_ASIO_CONCURRENCY_HINT_SAFE`]
  55. [
  56. The default. The `io_context` provides full thread safety, and distinct
  57. I/O objects may be used from any thread.
  58. ]
  59. ]
  60. ]
  61. [teletype]
  62. The concurrency hint used by default-constructed `io_context` objects can be
  63. overridden at compile time by defining the `BOOST_ASIO_CONCURRENCY_HINT_DEFAULT`
  64. macro. For example, specifying
  65. -DBOOST_ASIO_CONCURRENCY_HINT_DEFAULT=1
  66. on the compiler command line means that a concurrency hint of `1` is used for
  67. all default-constructed `io_context` objects in the program. Similarly, the
  68. concurrency hint used by `io_context` objects constructed with `1` can be
  69. overridden by defining `BOOST_ASIO_CONCURRENCY_HINT_1`. For example, passing
  70. -DBOOST_ASIO_CONCURRENCY_HINT_1=BOOST_ASIO_CONCURRENCY_HINT_UNSAFE
  71. to the compiler will disable thread safety for all of these objects.
  72. [endsect]