1 #!/usr/local/bin/rune -x
3 # Demonstrate synchronous threads vs asynchronous threads. A threaded
4 # procedure is synchronous by default, meaning that it executes in an
5 # ultra-light-weight fashion using the machine context of the caller
6 # and only switching away if it blocks. For example, if you have a
7 # ton of GUI gadgets, each implemented as a thread, you may desire to
8 # implement them as synchronous entities within a single asynchronous
9 # container to reduce machine overhead.
11 # An asynchronous thread creates an actual machine thread to manage
17 alias stdio.File *stdout = stdio.stdout;
20 main(int ac, char **av)
24 stdout->setmode(stdio.File.M_FULL);
26 stdout->show(dosync("this is td=0000", 0));
27 stdout->show(dosync("this is td=0001", 1));
28 stdout->show(dosync("this is td=0002", 2));
29 stdout->show(dosync("this is td=0003", 3));
30 stdout->show(dosync("this is td=0004", 4));
31 stdout->show(dosync("this is td=0005", 5));
32 stdout->show(dosync("this is td=0006", 6));
33 stdout->show(dosync("this is td=0007", 7));
34 stdout->show(dosync("this is td=0008", 8));
35 stdout->show(dosync("this is td=0009", 9));
37 stdout->show(doasync("this is atd=0000", 0));
38 stdout->show(doasync("this is atd=0001", 1));
39 stdout->show(doasync("this is atd=0002", 2));
40 stdout->show(doasync("this is atd=0003", 3));
41 stdout->show(doasync("this is atd=0004", 4));
42 stdout->show(doasync("this is atd=0005", 5));
43 stdout->show(doasync("this is atd=0006", 6));
44 stdout->show(doasync("this is atd=0007", 7));
45 stdout->show(doasync("this is atd=0008", 8));
46 stdout->show(doasync("this is atd=0009", 9));
50 stdout->show(dosync("this is td=0000", 0));
51 stdout->show(dosync("this is td=0001", 1));
52 stdout->show(dosync("this is td=0002", 2));
53 stdout->show(dosync("this is td=0003", 3));
54 stdout->show(dosync("this is td=0004", 4));
55 stdout->show(dosync("this is td=0005", 5));
56 stdout->show(dosync("this is td=0006", 6));
57 stdout->show(dosync("this is td=0007", 7));
58 stdout->show(dosync("this is td=0008", 8));
59 stdout->show(dosync("this is td=0009", 9));
61 stdout->show(doasync("this is atd=0000", 0));
62 stdout->show(doasync("this is atd=0001", 1));
63 stdout->show(doasync("this is atd=0002", 2));
64 stdout->show(doasync("this is atd=0003", 3));
65 stdout->show(doasync("this is atd=0004", 4));
66 stdout->show(doasync("this is atd=0005", 5));
67 stdout->show(doasync("this is atd=0006", 6));
68 stdout->show(doasync("this is atd=0007", 7));
69 stdout->show(doasync("this is atd=0008", 8));
70 stdout->show(doasync("this is atd=0009", 9));
75 dosync(const char *str, int x)
79 const char *copy = str;
83 for (i = 0; i < 10000; ++i) {
84 stdout->show(copy, i);
85 thread_schedule immediate;
92 doasync(const char *str, int x)
96 const char *copy = str;
102 for (i = 0; i < 10000; ++i) {
103 stdout->show(copy, i);