Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/concurrency/text/basics.tex

    r21a1efb r3628765  
    11% ======================================================================
    22% ======================================================================
    3 \chapter{Basics}\label{basics}
     3\chapter{Concurrency Basics}\label{basics}
    44% ======================================================================
    55% ======================================================================
    6 Before any detailed discussion of the concurrency and parallelism in \CFA, it is important to describe the basics of concurrency and how they are expressed in \CFA user code.
     6Before any detailed discussion of the concurrency and parallelism in \CFA, it is important to describe the basics of concurrency and how they are expressed in \CFA user-code.
    77
    88\section{Basics of concurrency}
    9 At its core, concurrency is based on having call-stacks and potentially multiple threads of execution for these stacks. Concurrency without parallelism only requires having multiple call stacks (or contexts) for a single thread of execution, and switching between these call stacks on a regular basis. A minimal concurrency product can be achieved by creating coroutines, which instead of context switching between each other, always ask an oracle where to context switch next. While coroutines do not technically require a stack, stackfull coroutines are the closest abstraction to a practical "naked"" call stack. When writing concurrency in terms of coroutines, the oracle effectively becomes a scheduler and the whole system now follows a cooperative threading-model \cit. The oracle/scheduler can either be a stackless or stackfull entity and correspondingly require one or two context switches to run a different coroutine. In any case, a subset of concurrency related challenges start to appear. For the complete set of concurrency challenges to occur, the only feature missing is preemption. Indeed, concurrency challenges appear with non-determinism. Guaranteeing mutual-exclusion or synchronisation are simply ways of limiting the lack of determinism in a system. A scheduler introduces order of execution uncertainty, while preemption introduces incertainty about where context-switches occur. Now it is important to understand that uncertainty is not necessarily undesireable; uncertainty can often be used by systems to significantly increase performance and is often the basis of giving a user the illusion that tasks are running in parallel. Optimal performance in concurrent applications is often obtained by having as much non-determinism as correctness allows\cit.
     9At its core, concurrency is based on having multiple call-stacks and scheduling among threads of execution executing on these stacks. Concurrency without parallelism only requires having multiple call stacks (or contexts) for a single thread of execution.
     10
     11Indeed, while execution with a single thread and multiple stacks where the thread is self-scheduling deterministically across the stacks is called coroutining, execution with a single and multiple stacks but where the thread is scheduled by an oracle (non-deterministic from the thread perspective) across the stacks is called concurrency.
     12
     13Therefore, a minimal concurrency system can be achieved by creating coroutines, which instead of context switching among each other, always ask an oracle where to context switch next. While coroutines can execute on the caller's stack-frame, stackfull coroutines allow full generality and are sufficient as the basis for concurrency. The aforementioned oracle is a scheduler and the whole system now follows a cooperative threading-model \cit. The oracle/scheduler can either be a stackless or stackfull entity and correspondingly require one or two context switches to run a different coroutine. In any case, a subset of concurrency related challenges start to appear. For the complete set of concurrency challenges to occur, the only feature missing is preemption. Indeed, concurrency challenges appear with non-determinism. Using mutual-exclusion or synchronisation are ways of limiting the lack of determinism in a system. A scheduler introduces order of execution uncertainty, while preemption introduces uncertainty about where context-switches occur. Now it is important to understand that uncertainty is not undesireable; uncertainty can often be used by systems to significantly increase performance and is often the basis of giving a user the illusion that tasks are running in parallel. Optimal performance in concurrent applications is often obtained by having as much non-determinism as correctness allows\cit.
    1014
    1115\section{\protect\CFA 's Thread Building Blocks}
    12 One of the important features that is missing in C is threading. On modern architectures, a lack of threading is becoming less and less forgivable\cite{Sutter05, Sutter05b}, and therefore modern programming languages must have the proper tools to allow users to write performant concurrent and/or parallel programs. As an extension of C, \CFA needs to express these concepts in a way that is as natural as possible to programmers used to imperative languages. And being a system-level language means programmers expect to choose precisely which features they need and which cost they are willing to pay.
     16One of the important features that is missing in C is threading. On modern architectures, a lack of threading is unacceptable\cite{Sutter05, Sutter05b}, and therefore modern programming languages must have the proper tools to allow users to write performant concurrent and/or parallel programs. As an extension of C, \CFA needs to express these concepts in a way that is as natural as possible to programmers familiar with imperative languages. And being a system-level language means programmers expect to choose precisely which features they need and which cost they are willing to pay.
    1317
    1418\section{Coroutines: A stepping stone}\label{coroutine}
    15 While the main focus of this proposal is concurrency and parallelism, as mentionned above it is important to adress coroutines, which are actually a significant underlying aspect of a concurrency system. Indeed, while having nothing to do with parallelism and arguably little to do with concurrency, coroutines need to deal with context-switchs and other context-management operations. Therefore, this proposal includes coroutines both as an intermediate step for the implementation of threads, and a first class feature of \CFA. Furthermore, many design challenges of threads are at least partially present in designing coroutines, which makes the design effort that much more relevant. The core API of coroutines revolve around two features: independent call stacks and \code{suspend}/\code{resume}.
    16 
    17 Here is an example of a solution to the fibonnaci problem using \CFA coroutines:
    18 \begin{cfacode}
    19         coroutine Fibonacci {
    20               int fn; // used for communication
    21         };
    22 
    23         void ?{}(Fibonacci & this) { // constructor
    24               this.fn = 0;
    25         }
    26 
    27         // main automacically called on first resume
    28         void main(Fibonacci & this) {
    29                 int fn1, fn2;           // retained between resumes
    30                 this.fn = 0;
    31                 fn1 = this.fn;
    32                 suspend(this);          // return to last resume
    33 
    34                 this.fn = 1;
     19While the main focus of this proposal is concurrency and parallelism, it is important to address coroutines, which are actually a significant building block of a concurrency system. Coroutines need to deal with context-switchs and other context-management operations. Therefore, this proposal includes coroutines both as an intermediate step for the implementation of threads, and a first class feature of \CFA. Furthermore, many design challenges of threads are at least partially present in designing coroutines, which makes the design effort that much more relevant. The core \acrshort{api} of coroutines revolve around two features: independent call stacks and \code{suspend}/\code{resume}.
     20
     21A good example of a problem made easier with coroutines is genereting the fibonacci sequence. This problem comes with the challenge of decoupling how a sequence is generated and how it is used. Figure \ref{fig:fibonacci-c} shows conventional approaches to writing generators in C. All three of these approach suffer from strong coupling. The left and center approaches require that the generator have knowledge of how the sequence will be used, while the rightmost approach requires to user to hold internal state between calls on behalf of th sequence generator and makes it much harder to handle corner cases like the Fibonacci seed.
     22\begin{figure}
     23\label{fig:fibonacci-c}
     24\caption{Different implementations of a fibonacci sequence generator in C.}
     25\begin{center}
     26\begin{tabular}{c @{\hskip 0.025in}|@{\hskip 0.025in} c @{\hskip 0.025in}|@{\hskip 0.025in} c}
     27\begin{ccode}[tabsize=2]
     28//Using callbacks
     29void fibonacci_func(
     30        int n,
     31        void (*callback)(int)
     32) {
     33        int first = 0;
     34        int second = 1;
     35        int next, i;
     36        for(i = 0; i < n; i++)
     37        {
     38                if(i <= 1)
     39                        next = i;
     40                else {
     41                        next = f1 + f2;
     42                        f1 = f2;
     43                        f2 = next;
     44                }
     45                callback(next);
     46        }
     47}
     48\end{ccode}&\begin{ccode}[tabsize=2]
     49//Using output array
     50void fibonacci_array(
     51        int n,
     52        int * array
     53) {
     54        int f1 = 0; int f2 = 1;
     55        int next, i;
     56        for(i = 0; i < n; i++)
     57        {
     58                if(i <= 1)
     59                        next = i;
     60                else {
     61                        next = f1 + f2;
     62                        f1 = f2;
     63                        f2 = next;
     64                }
     65                *array = next;
     66                array++;
     67        }
     68}
     69\end{ccode}&\begin{ccode}[tabsize=2]
     70//Using external state
     71typedef struct {
     72        int f1, f2;
     73} iterator_t;
     74
     75int fibonacci_state(
     76        iterator_t * it
     77) {
     78        int f;
     79        f = it->f1 + it->f2;
     80        it->f2 = it->f1;
     81        it->f1 = f;
     82        return f;
     83}
     84
     85
     86
     87
     88
     89
     90\end{ccode}
     91\end{tabular}
     92\end{center}
     93\end{figure}
     94
     95
     96Figure \ref{fig:fibonacci-cfa} is an example of a solution to the fibonnaci problem using \CFA coroutines, using the coroutine stack to hold sufficient state for the generation. This solution has the advantage of having very strong decoupling between how the sequence is generated and how it is used. Indeed, this version is a easy to use as the \code{fibonacci_state} solution, while the imlpementation is very similar to the \code{fibonacci_func} example.
     97
     98\begin{figure}
     99\label{fig:fibonacci-cfa}
     100\caption{Implementation of fibonacci using coroutines}
     101\begin{cfacode}
     102coroutine Fibonacci {
     103        int fn; //used for communication
     104};
     105
     106void ?{}(Fibonacci & this) { //constructor
     107        this.fn = 0;
     108}
     109
     110//main automacically called on first resume
     111void main(Fibonacci & this) {
     112        int fn1, fn2;           //retained between resumes
     113        this.fn = 0;
     114        fn1 = this.fn;
     115        suspend(this);          //return to last resume
     116
     117        this.fn = 1;
     118        fn2 = fn1;
     119        fn1 = this.fn;
     120        suspend(this);          //return to last resume
     121
     122        for ( ;; ) {
     123                this.fn = fn1 + fn2;
    35124                fn2 = fn1;
    36125                fn1 = this.fn;
    37                 suspend(this);          // return to last resume
    38 
    39                 for ( ;; ) {
    40                         this.fn = fn1 + fn2;
    41                         fn2 = fn1;
    42                         fn1 = this.fn;
    43                         suspend(this);  // return to last resume
    44                 }
    45         }
    46 
    47         int next(Fibonacci & this) {
    48                 resume(this); // transfer to last suspend
    49                 return this.fn;
    50         }
    51 
    52         void main() { // regular program main
    53                 Fibonacci f1, f2;
    54                 for ( int i = 1; i <= 10; i += 1 ) {
    55                         sout | next( f1 ) | next( f2 ) | endl;
    56                 }
    57         }
    58 \end{cfacode}
     126                suspend(this);  //return to last resume
     127        }
     128}
     129
     130int next(Fibonacci & this) {
     131        resume(this); //transfer to last suspend
     132        return this.fn;
     133}
     134
     135void main() { //regular program main
     136        Fibonacci f1, f2;
     137        for ( int i = 1; i <= 10; i += 1 ) {
     138                sout | next( f1 ) | next( f2 ) | endl;
     139        }
     140}
     141\end{cfacode}
     142\end{figure}
    59143
    60144\subsection{Construction}
    61 One important design challenge for coroutines and threads (shown in section \ref{threads}) is that the runtime system needs to run code after the user-constructor runs. In the case of coroutines, this challenge is simpler since there is no non-determinism from preemption or scheduling. However, the underlying challenge remains the same for coroutines and threads.
    62 
    63 The runtime system needs to create the coroutine's stack and more importantly prepare it for the first resumption. The timing of the creation is non-trivial since users both expect to have fully constructed objects once execution enters the coroutine main and to be able to resume the coroutine from the constructor. Like for regular objects, constructors can still leak coroutines before they are ready. There are several solutions to this problem but the chosen options effectively forces the design of the coroutine.
     145One important design challenge for coroutines and threads (shown in section \ref{threads}) is that the runtime system needs to run code after the user-constructor runs to connect the object into the system. In the case of coroutines, this challenge is simpler since there is no non-determinism from preemption or scheduling. However, the underlying challenge remains the same for coroutines and threads.
     146
     147The runtime system needs to create the coroutine's stack and more importantly prepare it for the first resumption. The timing of the creation is non-trivial since users both expect to have fully constructed objects once execution enters the coroutine main and to be able to resume the coroutine from the constructor. As regular objects, constructors can leak coroutines before they are ready. There are several solutions to this problem but the chosen options effectively forces the design of the coroutine.
    64148
    65149Furthermore, \CFA faces an extra challenge as polymorphic routines create invisible thunks when casted to non-polymorphic routines and these thunks have function scope. For example, the following code, while looking benign, can run into undefined behaviour because of thunks:
     
    78162}
    79163\end{cfacode}
     164
    80165The generated C code\footnote{Code trimmed down for brevity} creates a local thunk to hold type information:
    81166
     
    95180}
    96181\end{ccode}
    97 The problem in this example is a race condition between the start of the execution of \code{noop} on the other thread and the stack frame of \code{bar} being destroyed. This extra challenge limits which solutions are viable because storing the function pointer for too long only increases the chances that the race will end in undefined behavior; i.e. the stack based thunk being destroyed before it was used. This challenge is an extension of challenges that come with second-class routines. Indeed, GCC nested routines also have the limitation that the routines cannot be passed outside of the scope of the functions these were declared in. The case of coroutines and threads is simply an extension of this problem to multiple call-stacks.
     182The problem in this example is a storage management issue, the function pointer \code{_thunk0} is only valid until the end of the block. This extra challenge limits which solutions are viable because storing the function pointer for too long causes undefined behavior; i.e. the stack based thunk being destroyed before it was used. This challenge is an extension of challenges that come with second-class routines. Indeed, GCC nested routines also have the limitation that the routines cannot be passed outside of the scope of the functions these were declared in. The case of coroutines and threads is simply an extension of this problem to multiple call-stacks.
    98183
    99184\subsection{Alternative: Composition}
    100 One solution to this challenge would be to use composition/containement,
    101 
    102 \begin{cfacode}
    103         struct Fibonacci {
    104               int fn; // used for communication
    105               coroutine c; //composition
    106         };
    107 
    108         void ?{}(Fibonacci & this) {
    109               this.fn = 0;
    110                 (this.c){};
    111         }
    112 \end{cfacode}
    113 There are two downsides to this approach. The first, which is relatively minor, is that the base class needs to be made aware of the main routine pointer, regardless of whether a parameter or a virtual pointer is used, this means the coroutine data must be made larger to store a value that is actually a compile time constant (address of the main routine). The second problem, which is both subtle and significant, is that now users can get the initialisation order of there coroutines wrong. Indeed, every field of a \CFA struct is constructed but in declaration order, unless users explicitly write otherwise. This semantics means that users who forget to initialize a the coroutine may resume the coroutine with an uninitilized object. For coroutines, this is unlikely to be a problem, for threads however, this is a significant problem.
     185One solution to this challenge is to use composition/containement, where uses add insert a coroutine field which contains the necessary information to manage the coroutine.
     186
     187\begin{cfacode}
     188struct Fibonacci {
     189        int fn; //used for communication
     190        coroutine c; //composition
     191};
     192
     193void ?{}(Fibonacci & this) {
     194        this.fn = 0;
     195        (this.c){}; //Call constructor to initialize coroutine
     196}
     197\end{cfacode}
     198There are two downsides to this approach. The first, which is relatively minor, made aware of the main routine pointer. This information must either be store in the coroutine runtime data or in its static type structure. When using composition, all coroutine handles have the same static type structure which means the pointer to the main needs to be part of the runtime data. This requirement means the coroutine data must be made larger to store a value that is actually a compile time constant (address of the main routine). The second problem, which is both subtle and significant, is that now users can get the initialisation order of coroutines wrong. Indeed, every field of a \CFA struct is constructed but in declaration order, unless users explicitly write otherwise. This semantics means that users who forget to initialize the coroutine handle may resume the coroutine with an uninitilized object. For coroutines, this is unlikely to be a problem, for threads however, this is a significant problem. Figure \ref{fig:fmt-line} shows the \code{Format} coroutine which rearranges text in order to group characters into blocks of fixed size. This is a good example where the control flow is made much simpler from being able to resume the coroutine from the constructor and highlights the idea that interesting control flow can occor in the constructor.
     199\begin{figure}
     200\label{fig:fmt-line}
     201\caption{Formatting text into lines of 5 blocks of 4 characters.}
     202\begin{cfacode}[tabsize=3]
     203//format characters into blocks of 4 and groups of 5 blocks per line
     204coroutine Format {
     205        char ch;                                                                        //used for communication
     206        int g, b;                                                               //global because used in destructor
     207};
     208
     209void  ?{}(Format & fmt) {
     210        resume( fmt );                                                  //prime (start) coroutine
     211}
     212
     213void ^?{}(Format & fmt) with fmt {
     214        if ( fmt.g != 0 || fmt.b != 0 )
     215        sout | endl;
     216}
     217
     218void main(Format & fmt) with fmt {
     219        for ( ;; ) {                                                    //for as many characters
     220                for(g = 0; g < 5; g++) {                //groups of 5 blocks
     221                        for(b = 0; b < 4; fb++) {       //blocks of 4 characters
     222                                suspend();
     223                                sout | ch;                                      //print character
     224                        }
     225                        sout | "  ";                                    //print block separator
     226                }
     227                sout | endl;                                            //print group separator
     228        }
     229}
     230
     231void prt(Format & fmt, char ch) {
     232        fmt.ch = ch;
     233        resume(fmt);
     234}
     235
     236int main() {
     237        Format fmt;
     238        char ch;
     239        Eof: for ( ;; ) {                                               //read until end of file
     240                sin | ch;                                                       //read one character
     241                if(eof(sin)) break Eof;                 //eof ?
     242                prt(fmt, ch);                                           //push character for formatting
     243        }
     244}
     245\end{cfacode}
     246\end{figure}
     247
    114248
    115249\subsection{Alternative: Reserved keyword}
     
    117251
    118252\begin{cfacode}
    119         coroutine Fibonacci {
    120               int fn; // used for communication
    121         };
    122 \end{cfacode}
    123 This mean the compiler can solve problems by injecting code where needed. The downside of this approach is that it makes coroutine a special case in the language. Users who would want to extend coroutines or build their own for various reasons can only do so in ways offered by the language. Furthermore, implementing coroutines without language supports also displays the power of \CFA.
    124 While this is ultimately the option used for idiomatic \CFA code, coroutines and threads can both be constructed by users without using the language support. The reserved keywords are only present to improve ease of use for the common cases.
     253coroutine Fibonacci {
     254        int fn; //used for communication
     255};
     256\end{cfacode}
     257This mean the compiler can solve problems by injecting code where needed. The downside of this approach is that it makes coroutine a special case in the language. Users who would want to extend coroutines or build their own for various reasons can only do so in ways offered by the language. Furthermore, implementing coroutines without language supports also displays the power of the programming language used. While this is ultimately the option used for idiomatic \CFA code, coroutines and threads can both be constructed by users without using the language support. The reserved keywords are only present to improve ease of use for the common cases.
    125258
    126259\subsection{Alternative: Lamda Objects}
     
    159292      coroutine_desc * get_coroutine(T & this);
    160293};
    161 \end{cfacode}
    162 This ensures an object is not a coroutine until \code{resume} (or \code{prime}) is called on the object. Correspondingly, any object that is passed to \code{resume} is a coroutine since it must satisfy the \code{is_coroutine} trait to compile. The advantage of this approach is that users can easily create different types of coroutines, for example, changing the memory foot print of a coroutine is trivial when implementing the \code{get_coroutine} routine. The \CFA keyword \code{coroutine} only has the effect of implementing the getter and forward declarations required for users to only have to implement the main routine.
     294
     295forall( dtype T | is_coroutine(T) ) void suspend(T &);
     296forall( dtype T | is_coroutine(T) ) void resume (T &);
     297\end{cfacode}
     298This ensures an object is not a coroutine until \code{resume} is called on the object. Correspondingly, any object that is passed to \code{resume} is a coroutine since it must satisfy the \code{is_coroutine} trait to compile. The advantage of this approach is that users can easily create different types of coroutines, for example, changing the memory layout of a coroutine is trivial when implementing the \code{get_coroutine} routine. The \CFA keyword \code{coroutine} only has the effect of implementing the getter and forward declarations required for users to only have to implement the main routine.
    163299
    164300\begin{center}
     
    186322\end{center}
    187323
    188 The combination of these two approaches allows users new to concurrency to have a easy and concise method while more advanced users can expose themselves to otherwise hidden pitfalls at the benefit of tighter control on memory layout and initialization.
     324The combination of these two approaches allows users new to coroutinning and concurrency to have an easy and concise specification, while more advanced users have tighter control on memory layout and initialization.
    189325
    190326\section{Thread Interface}\label{threads}
     
    205341\end{cfacode}
    206342
    207 Obviously, for this thread implementation to be usefull it must run some user code. Several other threading interfaces use a function-pointer representation as the interface of threads (for example \Csharp~\cite{Csharp} and Scala~\cite{Scala}). However, this proposal considers that statically tying a \code{main} routine to a thread superseeds this approach. Since the \code{main} routine is already a special routine in \CFA (where the program begins), it is possible naturally extend the semantics using overloading to declare mains for different threads (the normal main being the main of the initial thread). As such the \code{main} routine of a thread can be defined as
     343Obviously, for this thread implementation to be usefull it must run some user code. Several other threading interfaces use a function-pointer representation as the interface of threads (for example \Csharp~\cite{Csharp} and Scala~\cite{Scala}). However, this proposal considers that statically tying a \code{main} routine to a thread superseeds this approach. Since the \code{main} routine is already a special routine in \CFA (where the program begins), it is a natural extension of the semantics using overloading to declare mains for different threads (the normal main being the main of the initial thread). As such the \code{main} routine of a thread can be defined as
    208344\begin{cfacode}
    209345        thread foo {};
     
    214350\end{cfacode}
    215351
    216 In this example, threads of type \code{foo} start execution in the \code{void main(foo*)} routine which prints \code{"Hello World!"}. While this proposoal encourages this approach to enforce strongly-typed programming, users may prefer to use the routine based thread semantics for the sake of simplicity. With these semantics it is trivial to write a thread type that takes a function pointer as parameter and executes it on its stack asynchronously
    217 \begin{cfacode}
    218         typedef void (*voidFunc)(void);
     352In this example, threads of type \code{foo} start execution in the \code{void main(foo &)} routine, which prints \code{"Hello World!"}. While this thesis encourages this approach to enforce strongly-typed programming, users may prefer to use the routine-based thread semantics for the sake of simplicity. With these semantics it is trivial to write a thread type that takes a function pointer as a parameter and executes it on its stack asynchronously
     353\begin{cfacode}
     354        typedef void (*voidFunc)(int);
    219355
    220356        thread FuncRunner {
    221357                voidFunc func;
     358                int arg;
    222359        };
    223360
    224         //ctor
    225         void ?{}(FuncRunner & this, voidFunc inFunc) {
     361        void ?{}(FuncRunner & this, voidFunc inFunc, int arg) {
    226362                this.func = inFunc;
    227363        }
    228364
    229         //main
    230365        void main(FuncRunner & this) {
    231                 this.func();
    232         }
    233 \end{cfacode}
    234 
    235 An advantage of the overloading approach to main is to clearly highlight where and what memory is required to pass parameters and return values to/from a thread.
    236 
    237 Of course for threads to be useful, it must be possible to start and stop threads and wait for them to complete execution. While using an \acrshort{api} such as \code{fork} and \code{join} is relatively common in the literature, such an interface is unnecessary. Indeed, the simplest approach is to use \acrshort{raii} principles and have threads \code{fork} once the constructor has completed and \code{join} before the destructor runs.
     366                this.func( this.arg );
     367        }
     368\end{cfacode}
     369
     370An consequence of the strongly typed approach to main is that memory layout of parameters and return values to/from a thread are now explicitly specified in the \acrshort{api}.
     371
     372Of course for threads to be useful, it must be possible to start and stop threads and wait for them to complete execution. While using an \acrshort{api} such as \code{fork} and \code{join} is relatively common in the literature, such an interface is unnecessary. Indeed, the simplest approach is to use \acrshort{raii} principles and have threads \code{fork} after the constructor has completed and \code{join} before the destructor runs.
    238373\begin{cfacode}
    239374thread World;
     
    254389\end{cfacode}
    255390
    256 This semantic has several advantages over explicit semantics typesafety is guaranteed, a thread is always started and stopped exaclty once and users cannot make any progamming errors. Another advantage of this semantic is that it naturally scale to multiple threads meaning basic synchronisation is very simple
     391This semantic has several advantages over explicit semantics: a thread is always started and stopped exaclty once and users cannot make any progamming errors and it naturally scales to multiple threads meaning basic synchronisation is very simple
    257392
    258393\begin{cfacode}
     
    276411\end{cfacode}
    277412
    278 However, one of the apparent drawbacks of this system is that threads now always form a lattice, that is they are always destroyed in opposite order of construction because of block structure. However, storage allocation is not limited to blocks; dynamic allocation can create threads that outlive the scope in which the thread is created much like dynamically allocating memory lets objects outlive the scope in which they are created
     413However, one of the drawbacks of this approach is that threads now always form a lattice, that is they are always destroyed in opposite order of construction because of block structure. This restriction is relaxed by using dynamic allocation, so threads can outlive the scope in which they are created, much like dynamically allocating memory lets objects outlive the scope in which they are created
    279414
    280415\begin{cfacode}
     
    283418};
    284419
    285 //main
    286420void main(MyThread & this) {
    287421        //...
     
    291425        MyThread * long_lived;
    292426        {
     427                //Start a thread at the beginning of the scope
    293428                MyThread short_lived;
    294                 //Start a thread at the beginning of the scope
    295 
    296                 DoStuff();
    297429
    298430                //create another thread that will outlive the thread in this scope
    299431                long_lived = new MyThread;
    300432
     433                DoStuff();
     434
    301435                //Wait for the thread short_lived to finish
    302436        }
    303437        DoMoreStuff();
    304438
    305         //Now wait for the short_lived to finish
     439        //Now wait for the long_lived to finish
    306440        delete long_lived;
    307441}
Note: See TracChangeset for help on using the changeset viewer.