[d67cdb7] | 1 | % ======================================================================
|
---|
| 2 | % ======================================================================
|
---|
| 3 | \chapter{Putting it all together}
|
---|
| 4 | % ======================================================================
|
---|
| 5 | % ======================================================================
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | \section{Threads as monitors}
|
---|
[3364962] | 9 | As it was subtely alluded in section \ref{threads}, \code{threads} in \CFA are in fact monitors. This means that all the monitors features are available when using threads. For example, here is a very simple two thread pipeline that could be used for a simulator of a game engine :
|
---|
[d67cdb7] | 10 | \begin{cfacode}
|
---|
| 11 | // Visualization declaration
|
---|
| 12 | thread Renderer {} renderer;
|
---|
| 13 | Frame * simulate( Simulator & this );
|
---|
| 14 |
|
---|
| 15 | // Simulation declaration
|
---|
| 16 | thread Simulator{} simulator;
|
---|
| 17 | void render( Renderer & this );
|
---|
| 18 |
|
---|
| 19 | // Blocking call used as communication
|
---|
| 20 | void draw( Renderer & mutex this, Frame * frame );
|
---|
| 21 |
|
---|
| 22 | // Simualation loop
|
---|
| 23 | void main( Simulator & this ) {
|
---|
| 24 | while( true ) {
|
---|
| 25 | Frame * frame = simulate( this );
|
---|
| 26 | draw( renderer, frame );
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | // Rendering loop
|
---|
| 31 | void main( Renderer & this ) {
|
---|
| 32 | while( true ) {
|
---|
| 33 | waitfor( draw, this );
|
---|
| 34 | render( this );
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | \end{cfacode}
|
---|
| 38 | One of the obvious complaints of the previous code snippet (other than its toy-like simplicity) is that it does not handle exit conditions and just goes on for ever. Luckily, the monitor semantics can also be used to clearly enforce a shutdown order in a concise manner :
|
---|
| 39 | \begin{cfacode}
|
---|
| 40 | // Visualization declaration
|
---|
| 41 | thread Renderer {} renderer;
|
---|
| 42 | Frame * simulate( Simulator & this );
|
---|
| 43 |
|
---|
| 44 | // Simulation declaration
|
---|
| 45 | thread Simulator{} simulator;
|
---|
| 46 | void render( Renderer & this );
|
---|
| 47 |
|
---|
| 48 | // Blocking call used as communication
|
---|
| 49 | void draw( Renderer & mutex this, Frame * frame );
|
---|
| 50 |
|
---|
| 51 | // Simualation loop
|
---|
| 52 | void main( Simulator & this ) {
|
---|
| 53 | while( true ) {
|
---|
| 54 | Frame * frame = simulate( this );
|
---|
| 55 | draw( renderer, frame );
|
---|
| 56 |
|
---|
| 57 | // Exit main loop after the last frame
|
---|
| 58 | if( frame->is_last ) break;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // Rendering loop
|
---|
| 63 | void main( Renderer & this ) {
|
---|
| 64 | while( true ) {
|
---|
| 65 | waitfor( draw, this );
|
---|
| 66 | or waitfor( ^?{}, this ) {
|
---|
| 67 | // Add an exit condition
|
---|
| 68 | break;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | render( this );
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | \end{cfacode}
|
---|
| 75 |
|
---|
| 76 | \section{Fibers \& Threads}
|
---|