intro.qbk 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. [/
  2. Copyright Oliver Kowalke 2014.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt
  6. ]
  7. [section:intro Introduction]
  8. [heading Definition]
  9. In computer science routines are defined as a sequence of operations. The
  10. execution of routines forms a parent-child relationship and the child terminates
  11. always before the parent. Coroutines (the term was introduced by Melvin
  12. Conway [footnote Conway, Melvin E.. "Design of a Separable Transition-Diagram Compiler".
  13. Commun. ACM, Volume 6 Issue 7, July 1963, Article No. 7]),
  14. are a generalization of routines (Donald Knuth [footnote Knuth, Donald Ervin (1997).
  15. "Fundamental Algorithms. The Art of Computer Programming 1", (3rd ed.)]).
  16. The principal difference between coroutines and routines
  17. is that a coroutine enables explicit suspend and resume of its progress via
  18. additional operations by preserving execution state and thus provides an
  19. [*enhanced control flow] (maintaining the execution context).
  20. [heading How it works]
  21. Functions foo() and bar() are supposed to alternate their execution (leave and
  22. enter function body).
  23. [$../../../../libs/coroutine2/doc/images/foo_bar.png [align center]]
  24. If coroutines were called exactly like routines, the stack would grow with
  25. every call and would never be popped. A jump into the middle of a coroutine
  26. would not be possible, because the return address would be on top of
  27. stack entries.
  28. The solution is that each coroutine has its own stack and control-block
  29. (__fcontext__ from __boost_context__).
  30. Before the coroutine gets suspended, the non-volatile registers (including stack
  31. and instruction/program pointer) of the currently active coroutine are stored in
  32. the coroutine's control-block.
  33. The registers of the newly activated coroutine must be restored from its
  34. associated control-block before it is resumed.
  35. The context switch requires no system privileges and provides cooperative
  36. multitasking convenient to C++. Coroutines provide quasi parallelism.
  37. When a program is supposed to do several things at the same time, coroutines
  38. help to do this much more simply and elegantly than with only a single flow of
  39. control.
  40. The advantages can be seen particularly clearly with the use of a recursive
  41. function, such as traversal of binary trees (see example 'same fringe').
  42. [heading characteristics]
  43. Characteristics [footnote Moura, Ana Lucia De and Ierusalimschy, Roberto.
  44. "Revisiting coroutines". ACM Trans. Program. Lang. Syst., Volume 31 Issue 2,
  45. February 2009, Article No. 6] of a coroutine are:
  46. * values of local data persist between successive calls (context switches)
  47. * execution is suspended as control leaves coroutine and is resumed at certain time later
  48. * symmetric or asymmetric control-transfer mechanism; see below
  49. * first-class object (can be passed as argument, returned by procedures,
  50. stored in a data structure to be used later or freely manipulated by
  51. the developer)
  52. * stackful or stackless
  53. Coroutines are useful in simulation, artificial intelligence, concurrent
  54. programming, text processing and data manipulation, supporting
  55. the implementation of components such as cooperative tasks (fibers), iterators,
  56. generators, infinite lists, pipes etc.
  57. [heading execution-transfer mechanism]
  58. Two categories of coroutines exist: symmetric and asymmetric coroutines.
  59. An asymmetric coroutine knows its invoker, using a special operation to
  60. implicitly yield control specifically to its invoker. By contrast, all symmetric
  61. coroutines are equivalent; one symmetric coroutine may pass control to any
  62. other symmetric coroutine. Because of this, a symmetric coroutine ['must]
  63. specify the coroutine to which it intends to yield control.
  64. [$../../../../libs/coroutine2/doc/images/foo_bar_seq.png [align center]]
  65. Both concepts are equivalent and a fully-general coroutine library can provide
  66. either symmetric or asymmetric coroutines.
  67. [heading stackfulness]
  68. In contrast to a stackless coroutine a stackful coroutine can be suspended
  69. from within a nested stackframe. Execution resumes at exactly the same point
  70. in the code where it was suspended before.
  71. With a stackless coroutine, only the top-level routine may be suspended. Any
  72. routine called by that top-level routine may not itself suspend. This prohibits
  73. providing suspend/resume operations in routines within a general-purpose library.
  74. [heading first-class continuation]
  75. A first-class continuation can be passed as an argument, returned by a
  76. function and stored in a data structure to be used later.
  77. In some implementations (for instance C# ['yield]) the continuation can
  78. not be directly accessed or directly manipulated.
  79. Without stackfulness and first-class semantics, some useful execution control
  80. flows cannot be supported (for instance cooperative multitasking or
  81. checkpointing).
  82. [endsect]