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 int *totalCountP = &totalCount;
23 main(int ac, string_p *av)
27 stdout->setmode(stdio.File.M_FULL);
29 stdout->show(dosync("this is td=0000", 0));
30 stdout->show(dosync("this is td=0001", 1));
31 stdout->show(dosync("this is td=0002", 2));
32 stdout->show(dosync("this is td=0003", 3));
33 stdout->show(dosync("this is td=0004", 4));
34 stdout->show(dosync("this is td=0005", 5));
35 stdout->show(dosync("this is td=0006", 6));
36 stdout->show(dosync("this is td=0007", 7));
37 stdout->show(dosync("this is td=0008", 8));
38 stdout->show(dosync("this is td=0009", 9));
40 stdout->show(doasync("this is atd=0000", 0));
41 stdout->show(doasync("this is atd=0001", 1));
42 stdout->show(doasync("this is atd=0002", 2));
43 stdout->show(doasync("this is atd=0003", 3));
44 stdout->show(doasync("this is atd=0004", 4));
45 stdout->show(doasync("this is atd=0005", 5));
46 stdout->show(doasync("this is atd=0006", 6));
47 stdout->show(doasync("this is atd=0007", 7));
48 stdout->show(doasync("this is atd=0008", 8));
49 stdout->show(doasync("this is atd=0009", 9));
53 stdout->show(dosync("this is td=0000", 0));
54 stdout->show(dosync("this is td=0001", 1));
55 stdout->show(dosync("this is td=0002", 2));
56 stdout->show(dosync("this is td=0003", 3));
57 stdout->show(dosync("this is td=0004", 4));
58 stdout->show(dosync("this is td=0005", 5));
59 stdout->show(dosync("this is td=0006", 6));
60 stdout->show(dosync("this is td=0007", 7));
61 stdout->show(dosync("this is td=0008", 8));
62 stdout->show(dosync("this is td=0009", 9));
64 stdout->show(doasync("this is atd=0000", 0));
65 stdout->show(doasync("this is atd=0001", 1));
66 stdout->show(doasync("this is atd=0002", 2));
67 stdout->show(doasync("this is atd=0003", 3));
68 stdout->show(doasync("this is atd=0004", 4));
69 stdout->show(doasync("this is atd=0005", 5));
70 stdout->show(doasync("this is atd=0006", 6));
71 stdout->show(doasync("this is atd=0007", 7));
72 stdout->show(doasync("this is atd=0008", 8));
73 stdout->show(doasync("this is atd=0009", 9));
76 # Thread.mssleep(1000);
78 stdout->format("TotalCount %d\n", *totalCountP);
84 dosync(const char *str, int x)
88 const char *copy = str;
93 for (i = 0; i < 10000; ++i) {
94 # stdout->show(copy, i);
95 thread_schedule immediate;
102 doasync(const char *str, int x)
106 const char *copy = str;
113 for (i = 0; i < 10000; ++i) {
115 # stdout->show(copy, i);
118 # Thread.mssleep(1000);