Examples
These are examples of basic programs written in Cind language.
For information about program execution see Compilation and execution.
Hello World
execute() {
host.println("Hello World!");
}
Threads
execute() {
{# host.print('A'); # host.print('B'); # host.print('C');
# host.print('D'); # host.print('E'); # host.print('F'); };
host.println();
{#[20] host.print({# 0 # 1 }); };
host.println();
}
Selector
class selector holder {
start() {
while(true) {
accept send(var box) {
accept get() {
return box;
}
}
}
}
}
execute() {
var h = new holder();
{# for(int i=0; i<10; i++) {
h.send(i);
}
# for(int i=0; i<10; i++) {
var k = h.get();
host.print(k);
}
host.println();
}
}
99 bottles of beer
execute() {
// this class provides synchronization
// to get unique number of the bottle
class monitor giver {
int number = 100;
.get() { return --number; }
}
var g = new giver();
// start 99 concurrently worked threads
// each thread gets own number of the bottle and prints out its own verse
// (notice that the output lines from the threads will be mixed together)
{#[99]
int nr = g.get(); // get own number
host.println(nr," bottles of beer on the wall, ",nr," bottles of beer.");
host.print("Take one down, pass it around,");
if (nr > 1) {
host.println((nr-1)," bottles of beer on the wall.");
}
else {
host.println("no more bottles of beer on the wall.");
}
}
host.println("No more bottles of beer on the wall, no more bottles of beer.");
host.println("Go to the store and buy some more, 99 bottles of beer on the wall.");
return 0;
}
|
|