|
| 1 | +# core.base.thread |
| 2 | + |
| 3 | +Provides native thread support for concurrent programming, including thread creation, synchronization primitives, and inter-thread communication. |
| 4 | + |
| 5 | +## thread.start |
| 6 | + |
| 7 | +- Start a thread |
| 8 | + |
| 9 | +Creates and starts a thread to execute a callback function. |
| 10 | + |
| 11 | +```lua |
| 12 | +local t = thread.start(callback_function, ...) |
| 13 | +``` |
| 14 | + |
| 15 | +### Parameters |
| 16 | + |
| 17 | +- `callback`: Callback function to execute in the thread |
| 18 | +- `...`: Additional arguments passed to the callback function |
| 19 | + |
| 20 | +### Return Value |
| 21 | + |
| 22 | +Returns a thread object that can be used to wait for thread completion. |
| 23 | + |
| 24 | +## thread.start_named |
| 25 | + |
| 26 | +- Start a named thread |
| 27 | + |
| 28 | +Creates and starts a new thread with the specified name and callback function. |
| 29 | + |
| 30 | +```lua |
| 31 | +local t = thread.start_named("thread_name", callback_function, ...) |
| 32 | +``` |
| 33 | + |
| 34 | +### Parameters |
| 35 | + |
| 36 | +- `name`: Thread name |
| 37 | +- `callback`: Callback function to execute in the thread |
| 38 | +- `...`: Additional arguments passed to the callback function |
| 39 | + |
| 40 | +### Return Value |
| 41 | + |
| 42 | +Returns a thread object that can be used to wait for thread completion. |
| 43 | + |
| 44 | +### Example |
| 45 | + |
| 46 | +```lua |
| 47 | +import("core.base.thread") |
| 48 | + |
| 49 | +function callback(id) |
| 50 | + import("core.base.thread") |
| 51 | + print("%s: %d starting ..", thread.running(), id) |
| 52 | + for i = 1, 10 do |
| 53 | + print("%s: %d", thread.running(), i) |
| 54 | + os.sleep(1000) |
| 55 | + end |
| 56 | + print("%s: %d end", thread.running(), id) |
| 57 | +end |
| 58 | + |
| 59 | +function main() |
| 60 | + local t0 = thread.start_named("thread_0", callback, 0) |
| 61 | + local t1 = thread.start_named("thread_1", callback, 1) |
| 62 | + t0:wait(-1) |
| 63 | + t1:wait(-1) |
| 64 | +end |
| 65 | +``` |
| 66 | + |
| 67 | +## thread.running |
| 68 | + |
| 69 | +- Get the current thread name |
| 70 | + |
| 71 | +Returns the name of the currently running thread. |
| 72 | + |
| 73 | +```lua |
| 74 | +local name = thread.running() |
| 75 | +``` |
| 76 | + |
| 77 | +### Return Value |
| 78 | + |
| 79 | +Returns the name of the current thread as a string. |
| 80 | + |
| 81 | +## thread.mutex |
| 82 | + |
| 83 | +- Create a mutex object |
| 84 | + |
| 85 | +Creates a new mutex for thread synchronization. |
| 86 | + |
| 87 | +```lua |
| 88 | +local mutex = thread.mutex() |
| 89 | +``` |
| 90 | + |
| 91 | +### Return Value |
| 92 | + |
| 93 | +Returns a mutex object with the following methods: |
| 94 | + |
| 95 | +- `mutex:lock()`: Lock the mutex |
| 96 | +- `mutex:unlock()`: Unlock the mutex |
| 97 | + |
| 98 | +### Example |
| 99 | + |
| 100 | +```lua |
| 101 | +import("core.base.thread") |
| 102 | + |
| 103 | +function callback(mutex) |
| 104 | + import("core.base.thread") |
| 105 | + print("%s: starting ..", thread.running()) |
| 106 | + for i = 1, 10 do |
| 107 | + mutex:lock() |
| 108 | + print("%s: %d", thread.running(), i) |
| 109 | + mutex:unlock() |
| 110 | + os.sleep(1000) |
| 111 | + end |
| 112 | + print("%s: end", thread.running()) |
| 113 | +end |
| 114 | + |
| 115 | +function main() |
| 116 | + local mutex = thread.mutex() |
| 117 | + local t0 = thread.start_named("thread_0", callback, mutex) |
| 118 | + local t1 = thread.start_named("thread_1", callback, mutex) |
| 119 | + t0:wait(-1) |
| 120 | + t1:wait(-1) |
| 121 | +end |
| 122 | +``` |
| 123 | + |
| 124 | +## thread.event |
| 125 | + |
| 126 | +- Create an event object |
| 127 | + |
| 128 | +Creates a new event for thread signaling and synchronization. |
| 129 | + |
| 130 | +```lua |
| 131 | +local event = thread.event() |
| 132 | +``` |
| 133 | + |
| 134 | +### Return Value |
| 135 | + |
| 136 | +Returns an event object with the following methods: |
| 137 | + |
| 138 | +- `event:wait(timeout)`: Wait for the event to be signaled |
| 139 | +- `event:post()`: Signal the event |
| 140 | + |
| 141 | +### Parameters |
| 142 | + |
| 143 | +- `timeout`: Timeout in milliseconds (-1 for infinite wait) |
| 144 | + |
| 145 | +### Example |
| 146 | + |
| 147 | +```lua |
| 148 | +import("core.base.thread") |
| 149 | + |
| 150 | +function callback(event) |
| 151 | + import("core.base.thread") |
| 152 | + print("%s: starting ..", thread.running()) |
| 153 | + while true do |
| 154 | + print("%s: waiting ..", thread.running()) |
| 155 | + if event:wait(-1) > 0 then |
| 156 | + print("%s: triggered", thread.running()) |
| 157 | + end |
| 158 | + end |
| 159 | +end |
| 160 | + |
| 161 | +function main() |
| 162 | + local event = thread.event() |
| 163 | + local t = thread.start_named("keyboard", callback, event) |
| 164 | + while true do |
| 165 | + local ch = io.read() |
| 166 | + if ch then |
| 167 | + event:post() |
| 168 | + end |
| 169 | + end |
| 170 | + t:wait(-1) |
| 171 | +end |
| 172 | +``` |
| 173 | + |
| 174 | +## thread.semaphore |
| 175 | + |
| 176 | +- Create a semaphore object |
| 177 | + |
| 178 | +Creates a new semaphore for thread synchronization and resource counting. |
| 179 | + |
| 180 | +```lua |
| 181 | +local semaphore = thread.semaphore(name, initial_count) |
| 182 | +``` |
| 183 | + |
| 184 | +### Parameters |
| 185 | + |
| 186 | +- `name`: Semaphore name |
| 187 | +- `initial_count`: Initial count value |
| 188 | + |
| 189 | +### Return Value |
| 190 | + |
| 191 | +Returns a semaphore object with the following methods: |
| 192 | + |
| 193 | +- `semaphore:wait(timeout)`: Wait for semaphore (decrement count) |
| 194 | +- `semaphore:post(count)`: Post to semaphore (increment count) |
| 195 | + |
| 196 | +### Example |
| 197 | + |
| 198 | +```lua |
| 199 | +import("core.base.thread") |
| 200 | + |
| 201 | +function callback(semaphore) |
| 202 | + import("core.base.thread") |
| 203 | + print("%s: starting ..", thread.running()) |
| 204 | + while true do |
| 205 | + print("%s: waiting ..", thread.running()) |
| 206 | + if semaphore:wait(-1) > 0 then |
| 207 | + print("%s: triggered", thread.running()) |
| 208 | + end |
| 209 | + end |
| 210 | +end |
| 211 | + |
| 212 | +function main() |
| 213 | + local semaphore = thread.semaphore("", 1) |
| 214 | + local t = thread.start_named("keyboard", callback, semaphore) |
| 215 | + while true do |
| 216 | + local ch = io.read() |
| 217 | + if ch then |
| 218 | + semaphore:post(2) |
| 219 | + end |
| 220 | + end |
| 221 | + t:wait(-1) |
| 222 | +end |
| 223 | +``` |
| 224 | + |
| 225 | +## thread.queue |
| 226 | + |
| 227 | +- Create a thread-safe queue object |
| 228 | + |
| 229 | +Creates a new thread-safe queue for inter-thread data communication. |
| 230 | + |
| 231 | +```lua |
| 232 | +local queue = thread.queue() |
| 233 | +``` |
| 234 | + |
| 235 | +### Return Value |
| 236 | + |
| 237 | +Returns a queue object with the following methods: |
| 238 | + |
| 239 | +- `queue:push(value)`: Push a value to the queue |
| 240 | +- `queue:pop()`: Pop a value from the queue |
| 241 | +- `queue:empty()`: Check if the queue is empty |
| 242 | + |
| 243 | +### Example |
| 244 | + |
| 245 | +```lua |
| 246 | +import("core.base.thread") |
| 247 | + |
| 248 | +function callback(event, queue) |
| 249 | + print("starting ..") |
| 250 | + while true do |
| 251 | + print("waiting ..") |
| 252 | + if event:wait(-1) > 0 then |
| 253 | + while not queue:empty() do |
| 254 | + print(" -> %s", queue:pop()) |
| 255 | + end |
| 256 | + end |
| 257 | + end |
| 258 | +end |
| 259 | + |
| 260 | +function main() |
| 261 | + local event = thread.event() |
| 262 | + local queue = thread.queue() |
| 263 | + local t = thread.start_named("", callback, event, queue) |
| 264 | + while true do |
| 265 | + local ch = io.read() |
| 266 | + if ch then |
| 267 | + queue:push(ch) |
| 268 | + event:post() |
| 269 | + end |
| 270 | + end |
| 271 | + t:wait(-1) |
| 272 | +end |
| 273 | +``` |
| 274 | + |
| 275 | +## thread.sharedata |
| 276 | + |
| 277 | +- Create a shared data object |
| 278 | + |
| 279 | +Creates a new shared data object for inter-thread data sharing. |
| 280 | + |
| 281 | +```lua |
| 282 | +local sharedata = thread.sharedata() |
| 283 | +``` |
| 284 | + |
| 285 | +### Return Value |
| 286 | + |
| 287 | +Returns a shared data object with the following methods: |
| 288 | + |
| 289 | +- `sharedata:set(value)`: Set the shared data value |
| 290 | +- `sharedata:get()`: Get the shared data value |
| 291 | + |
| 292 | +### Example |
| 293 | + |
| 294 | +```lua |
| 295 | +import("core.base.thread") |
| 296 | + |
| 297 | +function callback(event, sharedata) |
| 298 | + print("starting ..") |
| 299 | + while true do |
| 300 | + print("waiting ..") |
| 301 | + if event:wait(-1) > 0 then |
| 302 | + print(" -> %s", sharedata:get()) |
| 303 | + end |
| 304 | + end |
| 305 | +end |
| 306 | + |
| 307 | +function main() |
| 308 | + local event = thread.event() |
| 309 | + local sharedata = thread.sharedata() |
| 310 | + local t = thread.start_named("", callback, event, sharedata) |
| 311 | + while true do |
| 312 | + local ch = io.read() |
| 313 | + if ch then |
| 314 | + sharedata:set(ch) |
| 315 | + event:post() |
| 316 | + end |
| 317 | + end |
| 318 | + t:wait(-1) |
| 319 | +end |
| 320 | +``` |
| 321 | + |
| 322 | +## thread:wait |
| 323 | + |
| 324 | +- Wait for thread completion (thread instance method) |
| 325 | + |
| 326 | +Waits for the thread to complete execution. This method supports mixed scheduling with coroutines, allowing you to wait for thread completion within a coroutine. |
| 327 | + |
| 328 | +```lua |
| 329 | +thread:wait(timeout) |
| 330 | +``` |
| 331 | + |
| 332 | +### Parameters |
| 333 | + |
| 334 | +- `timeout`: Timeout in milliseconds (-1 for infinite wait) |
| 335 | + |
| 336 | +### Return Value |
| 337 | + |
| 338 | +Returns a status code indicating the wait result. |
| 339 | + |
| 340 | +### Example (Mixed Thread and Coroutine Scheduling) |
| 341 | + |
| 342 | +```lua |
| 343 | +import("core.base.thread") |
| 344 | +import("core.base.scheduler") |
| 345 | + |
| 346 | +function thread_loop() |
| 347 | + import("core.base.thread") |
| 348 | + print("%s: starting ..", thread.running()) |
| 349 | + for i = 1, 10 do |
| 350 | + print("%s: %d", thread.running(), i) |
| 351 | + os.sleep(1000) |
| 352 | + end |
| 353 | + print("%s: end", thread.running()) |
| 354 | +end |
| 355 | + |
| 356 | +function coroutine_loop() |
| 357 | + print("%s: starting ..", scheduler.co_running()) |
| 358 | + for i = 1, 10 do |
| 359 | + print("%s: %d", scheduler.co_running(), i) |
| 360 | + os.sleep(1000) |
| 361 | + end |
| 362 | + print("%s: end", scheduler.co_running()) |
| 363 | +end |
| 364 | + |
| 365 | +function main() |
| 366 | + scheduler.co_start_named("coroutine", coroutine_loop) |
| 367 | + local t = thread.start_named("thread", thread_loop) |
| 368 | + t:wait(-1) -- Wait for thread completion in coroutine |
| 369 | +end |
| 370 | +``` |
0 commit comments