Even the experts get threading wrong

dennis luehring dl.soluz at gmx.net
Thu Aug 7 05:15:59 PDT 2008


from the corn tutorial:

 Corn programming language is finally also an concurrent language (if not to say: it is mainly parallel language). It provides many different kinds of concurrency, from starting alone threads, internally catching messages by selector class, executing one code in concurrently way in the same context of variables, up to mixing everything together. It also offers many synchronization methods for accessing the same resources, by independent threads.

The first, and the simplest example is to divide one thread into two threads working concurrently with the same variables. Syntax of that construction is a block of instructions with special char ( # ) :

{# ...thread 1... ; ...thread 2... ; ... }

Examples of usage:


    let x = ... ,
        y = ... ,
	z = ...
    in {
	...
	{# { x = y } ; { x = z } };   // x will be either y or z
	x = {# y ; z };               // x will be either y or z

	{# { x = 1 ; y = x; };
	   { x = 2 };
	};                            // x will be either 1 or 2; y will be x or 1

	{# { x = y }; { y = x } };    // try to guess the result :)

	iftrue {# true; false } {
	    // 50% chance to be here
	}
	else {
	    // 50% chance to be here
	};

	x = y.message(# {x = y},{ x = z } );
	    // params will be prepared concurrently
	    // so, the possibilities are: (y,z),(y,y),(z,z),but never (z,y)
    }

The above instructions make concurrently worked threads, and wait for their execution. There is also instructions to make threads, but not waiting for them.



More information about the Digitalmars-d mailing list