This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
api:thread:zh [2023/10/20 13:12] hfsr [Thread Handle API] |
api:thread:zh [2023/10/20 13:18] hfsr [Thread Interrupt Handler Example] |
||
---|---|---|---|
Line 299: | Line 299: | ||
调用`thread.waitForAll({t})`在功能上与调用`t:join()`一致。在进程结束时会对其子线程调用`thread.waitForAll`(若有)。子线程也通过同样的机制阻塞其父进程。 | 调用`thread.waitForAll({t})`在功能上与调用`t:join()`一致。在进程结束时会对其子线程调用`thread.waitForAll`(若有)。子线程也通过同样的机制阻塞其父进程。 | ||
- | ===== Thread Exception Example ===== | + | ===== 线程异常样例 ===== |
- | This example demonstrates what happens when a thread throws an exception. A thread stops executing and becomes "dead" when it throws an uncaught exception. The exception message is not printed to stdout nor stderr (see `t:resume()`) for details. | + | 此样例演示了线程抛出异常时会发生什么。线程在抛出未捕获的异常时会停止执行并进入“死亡”状态。报错信息将不会输出到stdout或stderr(详见`t:resume()`)。 |
```lua | ```lua | ||
Line 310: | Line 310: | ||
local reader = thread.create(function() | local reader = thread.create(function() | ||
print("reader start") | print("reader start") | ||
- | error("thread abort") -- throws an exception | + | error("thread abort") -- 抛出一个异常 |
print("reader done") | print("reader done") | ||
end) | end) | ||
Line 316: | Line 316: | ||
``` | ``` | ||
- | Output | + | 输出: |
``` | ``` | ||
p start | p start | ||
Line 323: | Line 323: | ||
``` | ``` | ||
- | ===== Thread Interrupt Handler Example ===== | + | ===== 线程中断处理函数样例 ===== |
- | This example demonstrates how you would register a function that handles soft interrupts (^c) to close file handles, release resources, etc, and then exit the whole program. | + | 此样例演示了你该如何注册处理软中断(^c)的函数,用于关闭文件句柄、释放资源等等,然后退出整个程序。 |
```lua | ```lua | ||
Line 345: | Line 345: | ||
thread.waitForAny({cleanup_thread, main_thread}) | thread.waitForAny({cleanup_thread, main_thread}) | ||
- | os.exit(0) -- closes all remaining threads | + | os.exit(0) -- 关闭所有剩余线程 |
``` | ``` | ||
- | Assuming the user presses ^c to send an interrupt | + | 假设用户按下了^c发送了一次软中断 |
- | Output | + | |
+ | 输出: | ||
``` | ``` | ||
main program | main program |