This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
api:thread:zh [2023/10/20 11:05] hfsr [Thread API] |
api:thread:zh [2023/10/20 11:38] hfsr [Thread API] |
||
---|---|---|---|
Line 18: | Line 18: | ||
* 线程被手动暂停,参见`t:suspend()`。 | * 线程被手动暂停,参见`t:suspend()`。 | ||
- | ====Event Registration Independence==== | + | ====独立事件注册==== |
- | A thread maintains an independent set of event registrations; it does not inherit any and it does not share any. Any event registration made (e.g. listeners or timers) inside a thread belongs to that thread. | + | 每个线程都维护了一套独立的事件注册机制;不继承也不分享。线程中创建的所有事件注册记录(即侦听器或定时器)都只从属于此线程。 |
- | * When a thread dies all of its event registrations die with it. | + | * 线程结束时,其所有事件注册记录都会随之结束。 |
- | * A `suspended` thread ignores events (see `t:status() suspended`) | + | * `suspended`(暂停的)线程会忽略事件(参见`t:status() suspended`) |
- | * A thread cannot access/remove/interfere with another thread's event registrations. | + | * 一个线程不可以访问/移除/交互其他线程的事件注册记录。 |
- | * A pushed event is observed by all running threads on the system. | + | * 被推送的事件对系统中的所有运行中线程可见。 |
- | * Two separate threads can `event.pull` for the same event, and each thread will observe the event independently. | + | * 两个独立的线程可以对同一个事件`event.pull`,每个线程都会独立观测到此事件。 |
- | ===== Overview ===== | + | ===== 概述 ===== |
- | There are two main use cases for using threads over other viable options | + | 线程主要有两种用途是其他选择无法提供: |
- | - You want to write a function that makes blocking calls without blocking the rest of your application. | + | - 你需要编写一个函数,其中进行了阻塞式调用,但不阻塞程序其它部分的运行。 |
- | - You want a long running background function without having to manage yielding and resuming it manually. | + | - 你需要编写长期运行的后台函数,而不想手动管理其yield与resum。 |
- | ===== Functions ===== | + | ===== 函数 ===== |
- | There are two sections of API defined here. | + | 此处定义了两类API。 |
- | 1. The [[api:thread#Thread_API|thread]] api, or the static functions, provided by `require("thread")` | + | 1. [[api:thread:zh#线程API|线程]]API,或者说静态函数,由`require("thread")`提供。 |
- | 2. The [[api:thread#Thread_Handle_API|thread handle]] api, or the api available the thread _objects_ created by `thread.create`. In this documentation these thread handles will be denoted by just `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进程内而且不在任何线程内调用时不会有返回值。 |