- File:
-
- 1 edited
-
doc/proposals/concurrency/text/basics.tex (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/concurrency/text/basics.tex
r3364962 rfb31cb8 328 328 329 329 \begin{cfacode} 330 thread foo {};330 thread foo {}; 331 331 \end{cfacode} 332 332 … … 343 343 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 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 344 344 \begin{cfacode} 345 thread foo {};346 347 void main(foo & this) {348 sout | "Hello World!" | endl;349 }345 thread foo {}; 346 347 void main(foo & this) { 348 sout | "Hello World!" | endl; 349 } 350 350 \end{cfacode} 351 351 352 352 In 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 353 \begin{cfacode} 354 typedef void (*voidFunc)(int);355 356 thread FuncRunner {357 voidFunc func;358 int arg;359 };360 361 void ?{}(FuncRunner & this, voidFunc inFunc, int arg) {362 this.func = inFunc;363 }364 365 void main(FuncRunner & this) {366 this.func( this.arg );367 }354 typedef void (*voidFunc)(int); 355 356 thread FuncRunner { 357 voidFunc func; 358 int arg; 359 }; 360 361 void ?{}(FuncRunner & this, voidFunc inFunc, int arg) { 362 this.func = inFunc; 363 } 364 365 void main(FuncRunner & this) { 366 this.func( this.arg ); 367 } 368 368 \end{cfacode} 369 369
Note:
See TracChangeset
for help on using the changeset viewer.