Concurrent expressions
Concurrent block
Concurrent block
Threads are separated by hash character
First two characters of concurrent block
(because otherwise statement will be interpreted as a regular block of instructions).
Instructions of one thread can be separated by semicolon characters, just like in the block of code.
Example:
This instruction starts three threads, each of them will print out different character,
what can result in writting out one of six different sequences: ABC, ACB, BAC, BCA, CAB or CBA,
with equal probability.
In fact, process of the execution of concurrently worked threads most depends on the environment,
where Cind program are executed and on the software that manages its execution.
For instance, it depends on the number of cores in the phisical processor that executes program.
If there will be less free cores than executed threads, then they will be executed in parts in random order.
There is no way to know which thread will start firstly, which one ends last, which will be executed in parts, etc.,
so it's better to assume that everything could happen, and everything that can happen is equally probable.
The result value returned by a thread is a value from its last instruction,
or a value returned by instruction
The result returned by concurrent block is the result from the last ended thread.
For example, in this sentence:
there are two threads returning
because it is not known which one of those two threads will ends last.
Syntax of concurrent block also allows to execute a number of copies of given thread.
Number of copies must be putted in brackets
For example, this code:
executes five concurrent threads, each of them makes:
Another example:
will execute one thread making
Value putted inside brackets
When exception is thrown by some thread, a concurrent block will firstly wait for other threads to stops,
and after that, this exception will be thrown out from the concurrent block.
If more then one thread throws an exception, then concurrent block will throw only first received exception,
and the rest of them will be lost.
Separate thread
Separate thread statement
Instructions of thread can be separated by semicolon characters.
Example:
In above code, there are two threads, one of them executes
The separate thread inherits context from the place of declaration, including visible variables,
special objects and binded values.
The result returned by separate thread is being omitted, and any exception throwed out from the thread will be lost.
There is no method to stops working thread from the outside, except the situation when whole program is being terminated or suspended.
Incidentally, another way to start independent thread is to create object of selector class (see Selector class).
Accessing variables
Threads can access all variables, which are visible from the code (see Variables),
and can access them concurrently.
In example:
there are three concurrent threads, which are accessing the same variable
That code will result in printing out number -1, 0 or 1.
Reading and writing to variables are the only atomic instructions in Cind language.
Even variable increment operator
Synchronization methods
Monitor class and selector class are the primitive mechanisms to provide
sychronization between concurrently worked threads.
Monitor provides exclusive access to internal code, using execute locks and conditions
(see Monitor class);
while object of selector class has own thread, who can accept incoming messages in own way and order
(see Selector class).
Typical synchronization toys, like semaphores, mutexes, lock sections, etc., can be implemented
as an objects of monitor or selector class.