This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
api:thread:zh [2023/10/20 11:23] hfsr [Functions] |
api:thread:zh [2023/10/20 11:38] hfsr [Thread API] |
||
---|---|---|---|
Line 40: | Line 40: | ||
2. [[api:thread:zh#线程句柄API|线程句柄]]API,或者说适用于`thread.create`创建的线程_对象_的api。在此文档中这些线程句柄将会仅以`t`表示。 | 2. [[api:thread:zh#线程句柄API|线程句柄]]API,或者说适用于`thread.create`创建的线程_对象_的api。在此文档中这些线程句柄将会仅以`t`表示。 | ||
- | ===== Thread API ===== | + | ===== 线程API ===== |
- `thread.create(thread_proc: function[, ...]): table` | - `thread.create(thread_proc: function[, ...]): table` | ||
- | Starts a new thread executing the function `thread_proc` and returns its thread handle, see [[api:thread#Thread_Handle_API|Thread Handle API]]. This method takes an optional `...` which is passed to `thread_proc`. The runtime of the thread continues autonomously. | + | 开启一个执行函数`thread_proc`的新线程,并返回其线程句柄,参见[[api:thread:zh#线程句柄API|线程句柄API]]。此方法可接收可选的`...`参数,这些参数将会被传递给`thread_proc`。 线程将自动开始运行。 |
```lua | ```lua | ||
Line 57: | Line 57: | ||
``` | ``` | ||
- | Output: | + | 输出: |
``` | ``` | ||
Main program start | Main program start | ||
Line 67: | Line 67: | ||
- `thread.waitForAll(threads: table[, timeout: number]): boolean, string` | - `thread.waitForAll(threads: table[, timeout: number]): boolean, string` | ||
- | Waits for the array of `threads` to complete. This blocking call can return in `timeout` seconds if provided. Returns success and an error message on failure. A thread is "completed" under multiple conditions, see `t:join()` for details. | + | 等待给出的`threads`全部执行完成。此阻塞式操作可以在给出的`timeout`秒超时时间后返回,若给出此值的话。返回值为是否成功,若失败还会返回报错信息。线程可以在多种条件下被认定为”执行完成“,详见`t:join()`。 |
```lua | ```lua | ||
Line 81: | Line 81: | ||
``` | ``` | ||
- | Output: | + | 输出: |
``` | ``` | ||
Main program start | Main program start | ||
Line 91: | Line 91: | ||
- `thread.waitForAny(threads: table[, timeout: number): boolean, string` | - `thread.waitForAny(threads: table[, timeout: number): boolean, string` | ||
- | Waits for any single thread to complete and is otherwise equivalent to `thread.waitForAll()` | + | 等待给出线程中的某一个完成,其他方面`thread.waitForAll()`一致。 |
```lua | ```lua | ||
Line 111: | Line 111: | ||
``` | ``` | ||
- | Output: | + | 输出: |
``` | ``` | ||
Main program start | Main program start | ||
Line 121: | Line 121: | ||
``` | ``` | ||
- | Please note that threads resume order is not specified and this example may print "D" before it prints "Main program end" | + | 请注意线程恢复的顺序并未指定,此样例中也有可能在输出"Main program end"之前就输出"D"。 |
- `thread.current(): table` | - `thread.current(): table` | ||
- | Returns the current thread `t` object. The init process does not represent a thread and nothing is returned from this method if called from the init process and not inside any thread. | + | 返回当前线程对象`t`。init进程不代表任何线程,此函数在init进程内而且不在任何线程内调用时不会有返回值。 |