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:22] hfsr [Functions] |
||
---|---|---|---|
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 ===== | ===== Functions ===== | ||
Line 37: | Line 37: | ||
There are two sections of API defined here. | There are two sections of API defined here. | ||
- | 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 ===== | ===== Thread API ===== |