1 | # Creating threads |
---|
2 | |
---|
3 | Functions: |
---|
4 | |
---|
5 | #include "libthread/singthreads.h" |
---|
6 | |
---|
7 | typedef void *(*ThreadFunc)(ThreadState *thread, void *arg); |
---|
8 | ThreadState *createThread(ThreadFunc func, void *arg); |
---|
9 | void *joinThread(ThreadState *thread); |
---|
10 | |
---|
11 | Example: |
---|
12 | |
---|
13 | #include "libthread/singthreads.h" |
---|
14 | |
---|
15 | using namespace LibThread; |
---|
16 | |
---|
17 | void *thread_func(ThreadState *thread, void *arg) { |
---|
18 | ...; |
---|
19 | return result; |
---|
20 | } |
---|
21 | |
---|
22 | void example() { |
---|
23 | ThreadState *thread = createThread(func, NULL): |
---|
24 | void *result = joinThread(thread); |
---|
25 | // Note: do not delete thread |
---|
26 | } |
---|
27 | |
---|
28 | # Channels |
---|
29 | |
---|
30 | Classes and functions: |
---|
31 | |
---|
32 | #include "libthread/singthreads.h" |
---|
33 | #include "libthread/channel.h" |
---|
34 | |
---|
35 | template <typename T> |
---|
36 | class Channel { |
---|
37 | public: |
---|
38 | Channel(); |
---|
39 | void send(T& value); |
---|
40 | T receive(); |
---|
41 | void receive(T& result); |
---|
42 | } |
---|
43 | |
---|
44 | Example: |
---|
45 | |
---|
46 | #include "libthread/singthreads.h" |
---|
47 | #include "libthread/channel.h" |
---|
48 | |
---|
49 | using namespace LibThread; |
---|
50 | |
---|
51 | void example() { |
---|
52 | Channel<int> *ch = new Channel<int>(); |
---|
53 | ch->send(1); |
---|
54 | ch->send(2); |
---|
55 | int value = ch->receive(); |
---|
56 | ch->receive(value); // avoids copying |
---|
57 | delete ch; |
---|
58 | } |
---|
59 | |
---|
60 | # Synchronization variables |
---|
61 | |
---|
62 | Classes and functions: |
---|
63 | |
---|
64 | #include "libthread/singthreads.h" |
---|
65 | #include "libthread/syncvar.h" |
---|
66 | |
---|
67 | template <typename T> |
---|
68 | class SyncVar { |
---|
69 | public: |
---|
70 | SyncVar(); |
---|
71 | void write(T& value); |
---|
72 | void read(T& result); |
---|
73 | bool try_read(T& result); |
---|
74 | } |
---|
75 | |
---|
76 | Example: |
---|
77 | |
---|
78 | #include "libthread/singthreads.h" |
---|
79 | #include "libthread/channel.h" |
---|
80 | |
---|
81 | using namespace LibThread; |
---|
82 | |
---|
83 | void example() { |
---|
84 | SyncVar<int> *svar = new SyncVar<int>(); |
---|
85 | svar->write(1); |
---|
86 | int value; |
---|
87 | svar->read(value); |
---|
88 | delete svar; |
---|
89 | } |
---|
90 | |
---|
91 | # Mutexes and condition variables |
---|
92 | |
---|
93 | Classes and functions: |
---|
94 | |
---|
95 | #include "libthread/thread.h" |
---|
96 | |
---|
97 | class Lock { |
---|
98 | public: |
---|
99 | Lock(); |
---|
100 | void lock(); |
---|
101 | void unlock(); |
---|
102 | } |
---|
103 | |
---|
104 | class ConditionVariable { |
---|
105 | public: |
---|
106 | ConditionVariable(Lock *lock); |
---|
107 | void signal(); |
---|
108 | void wait(); |
---|
109 | void broadcast(); |
---|
110 | } |
---|
111 | |
---|
112 | Example: |
---|
113 | |
---|
114 | #include "libthread/thread.h" |
---|
115 | |
---|
116 | void example() { |
---|
117 | Lock *lock = new Lock(); |
---|
118 | ConditionVariable *cond = new ConditionVariable(lock); |
---|
119 | lock.lock(); |
---|
120 | cond.signal(); |
---|
121 | lock.unlock(); |
---|
122 | delete cond; |
---|
123 | delete lock; |
---|
124 | } |
---|
125 | |
---|
126 | # Semaphores |
---|
127 | |
---|
128 | Classes and functions: |
---|
129 | |
---|
130 | #include "libthread/thread.h" |
---|
131 | |
---|
132 | class Semaphore { |
---|
133 | public: |
---|
134 | Semaphore(); |
---|
135 | Semaphore(int initial_value); |
---|
136 | void post(); |
---|
137 | void wait(); |
---|
138 | } |
---|
139 | |
---|
140 | Example: |
---|
141 | |
---|
142 | #include "libthread/thread.h" |
---|
143 | |
---|
144 | void example() { |
---|
145 | Semaphore *sem = new Semaphore(1); |
---|
146 | sem->wait(); // -> 0 |
---|
147 | sem->post(); // -> 1 |
---|
148 | sem->wait(); // -> 0 |
---|
149 | delete sem; |
---|
150 | } |
---|
151 | |
---|