1 |
{ The fork() function can be used to "split" the respective script handler |
2 |
instance into multiple script handler instances which then continue to run |
3 |
on their own. So this is like creating new script "threads". As you can |
4 |
see in this example, as soon as the child threads have been forked, they |
5 |
continue to manage their own polyphonic variable instances, so polyphonic |
6 |
variables start to deviate in the individual threads after fork() as soon as |
7 |
threads are modifying the polyphonic variables. } |
8 |
|
9 |
on init |
10 |
declare polyphonic $i |
11 |
declare $k |
12 |
end on |
13 |
|
14 |
on note |
15 |
{ set the polyphonic variable to some arbitrary start value } |
16 |
$i := 10 |
17 |
|
18 |
{ create 2 sub-threads for this current script handler, and don't let the |
19 |
children be auto aborted when parent thread terminates } |
20 |
select fork(2, 0) |
21 |
case 0 |
22 |
{ will print "10" for $i } |
23 |
message("This is parent thread: $i=" & $i) |
24 |
|
25 |
$k := num_elements(%NKSP_CALLBACK_CHILD_ID) - 1 |
26 |
while ($k >= 0) |
27 |
message(" - ID of my child " & ($k+1) & " is: " & %NKSP_CALLBACK_CHILD_ID[$k]) |
28 |
dec($k) |
29 |
end while |
30 |
|
31 |
case 1 |
32 |
inc($i) |
33 |
|
34 |
{ will print "11" for $i } |
35 |
message("This is 1st child thread: $i=" & $i) |
36 |
|
37 |
message(" - ID of my parent is: " & $NKSP_CALLBACK_PARENT_ID) |
38 |
|
39 |
case 2 |
40 |
dec($i) |
41 |
|
42 |
{ will print "9" for $i } |
43 |
message("This is 2nd child thread: $i=" & $i) |
44 |
|
45 |
message(" - ID of my parent is: " & $NKSP_CALLBACK_PARENT_ID) |
46 |
|
47 |
end select |
48 |
|
49 |
message(" - my ID is: " & $NI_CALLBACK_ID) |
50 |
end on |