1#!./parrot
2# Copyright (C) 2012-2013, Parrot Foundation.
3
4=head1 NAME
5
6examples/threads/tasks.pir - Basic Task PMC examples
7
8=head1 SYNOPSIS
9
10    % ./parrot examples/threads/tasks.pir
11
12=head1 DESCRIPTION
13
14This is a basic example of using the Task PMC. At a high level, each Task PMC
15can be assigned a bit of code to run as a "task", as well as its own copy of
16data to operate on.
17
18Calling the schedule opcode on a Task PMC object is what starts the chain of
19execution.
20
21The output of this program will be a non-deterministic string of a's and b's.
22Their order will change on each execution.
23
24=cut
25
26.sub main :main
27    .local pmc task, a, b
28    task = get_global 'task'
29    a = new ['String']
30    a = "a"
31    b = new ['String']
32    b = "b"
33    $P0 = new ['Task']
34    setattribute $P0, 'code', task
35    setattribute $P0, 'data', a
36    $P1 = new ['Task']
37    setattribute $P1, 'code', task
38    setattribute $P1, 'data', b
39    schedule $P0
40    schedule $P1
41    sleep 10
42    exit 0
43.end
44
45.sub task
46    .param pmc name
47    .local int i
48start:
49    print name
50    i = 0
51loop:
52    inc i
53    if i >= 100000 goto start
54    goto loop
55.end
56
57# Local Variables:
58#   mode: pir
59#   fill-column: 100
60# End:
61# vim: expandtab shiftwidth=4 ft=pir:
62