| 1 | = Threads and Delegates = |
---|
| 2 | |
---|
| 3 | ''Part of'' [StandardLibraryCategory Standard Library Category] |
---|
| 4 | |
---|
| 5 | == Example == |
---|
| 6 | |
---|
| 7 | {{{ |
---|
| 8 | #!d |
---|
| 9 | import std.c.time; |
---|
| 10 | import std.stdio; |
---|
| 11 | import std.thread; |
---|
| 12 | |
---|
| 13 | class Foo |
---|
| 14 | { |
---|
| 15 | int _v; |
---|
| 16 | |
---|
| 17 | this(int n) |
---|
| 18 | { |
---|
| 19 | _v = n; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | public int glob() |
---|
| 23 | { |
---|
| 24 | return _v; |
---|
| 25 | } |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | class WorkerThread |
---|
| 29 | { |
---|
| 30 | int _v; |
---|
| 31 | |
---|
| 32 | this(int n) |
---|
| 33 | { |
---|
| 34 | _v = n; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | // Here's a private member function - we'll call this from the run() method |
---|
| 38 | void identify() |
---|
| 39 | { |
---|
| 40 | writefln("WorkerThread(%d)", _v); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | // When run is called via the delegate it will have the appropriate this |
---|
| 44 | // pointer, and so has access to all the fields and methods of its class. |
---|
| 45 | public int run() |
---|
| 46 | { |
---|
| 47 | for (;;) |
---|
| 48 | { |
---|
| 49 | sleep(2); |
---|
| 50 | identify(); |
---|
| 51 | } |
---|
| 52 | return 0; |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | // In the thread 2 case, we override the thread class, and provide the run method |
---|
| 57 | // explicitly |
---|
| 58 | class Thread2 : Thread |
---|
| 59 | { |
---|
| 60 | int _v; |
---|
| 61 | |
---|
| 62 | this(int n) |
---|
| 63 | { |
---|
| 64 | _v = n; |
---|
| 65 | } |
---|
| 66 | public int run() |
---|
| 67 | { |
---|
| 68 | for (;;) |
---|
| 69 | { |
---|
| 70 | sleep(2); |
---|
| 71 | writefln("Thread2 %d", _v);; |
---|
| 72 | } |
---|
| 73 | return 0; |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void bar(int delegate() a) |
---|
| 78 | { |
---|
| 79 | writefln("rv = %d", a()); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | // In the case of thread 3, this will serve as the run method, and |
---|
| 83 | // it will be called with the value of the second argument to the |
---|
| 84 | // thread constructor (the documentation is not correct in this respect). |
---|
| 85 | int x(void *p) |
---|
| 86 | { |
---|
| 87 | int *ip = cast(int*) p; |
---|
| 88 | for (;;) |
---|
| 89 | { |
---|
| 90 | sleep(2); |
---|
| 91 | writefln("Thread3 %d", *ip); |
---|
| 92 | } |
---|
| 93 | return 0; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | void main(char[][] args) |
---|
| 98 | { |
---|
| 99 | // Before we get to threads, these first few lines just illustrate the |
---|
| 100 | // general ideas of delegates as applied to class member functions. |
---|
| 101 | Foo foo = new Foo(42); |
---|
| 102 | int delegate() dg = &foo.glob; |
---|
| 103 | int n = dg(); |
---|
| 104 | writefln("n = %d", n); |
---|
| 105 | bar(dg); |
---|
| 106 | int n3 = 123; |
---|
| 107 | |
---|
| 108 | // Now the different ways of instantiating threads. |
---|
| 109 | WorkerThread wt = new WorkerThread(22); |
---|
| 110 | dg = &wt.run; |
---|
| 111 | Thread t1 = new Thread(dg); |
---|
| 112 | t1.start(); |
---|
| 113 | |
---|
| 114 | Thread2 t2 = new Thread2(321); |
---|
| 115 | t2.start(); |
---|
| 116 | |
---|
| 117 | Thread t3 = new Thread(&x, &n3); |
---|
| 118 | t3.start(); |
---|
| 119 | |
---|
| 120 | sleep(10); |
---|
| 121 | } |
---|
| 122 | }}} |
---|
| 123 | |
---|
| 124 | == Sample Batch File == |
---|
| 125 | |
---|
| 126 | {{{ |
---|
| 127 | @echo off |
---|
| 128 | set pgm=ThreadsAndDelegatesExample |
---|
| 129 | dmd %pgm%.d |
---|
| 130 | %pgm%.exe |
---|
| 131 | |
---|
| 132 | pause |
---|
| 133 | erase *.obj |
---|
| 134 | erase *.map |
---|
| 135 | }}} |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | == Tested Environment == |
---|
| 139 | |
---|
| 140 | Tested with Digital Mars D Compiler v1.014 on Windows 2000. |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | == Source == |
---|
| 144 | |
---|
| 145 | Based on "[http://www.dsource.org/forums/viewtopic.php?t=2752 Threads, delegates, and all that]" by teales. |