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 [2017/09/02 00:48] payonel [Thread Handle API] |
api:thread [2017/09/07 19:32] payonel [Thread Handle API] |
||
---|---|---|---|
Line 114: | Line 114: | ||
``` | ``` | ||
- | * Please note that threads resume order is not specified | + | Please note that threads resume order is not specified and this example may print "D" before it prints "Main program end" |
+ | |||
+ | - `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. | ||
+ | |||
===== Thread Handle API ===== | ===== Thread Handle API ===== | ||
Line 126: | Line 132: | ||
- `t:suspend(): boolean, string` | - `t:suspend(): boolean, string` | ||
- | Suspends (or freezes) a running thread. Returns success and an error message on failure. A "suspended" thread never autonomously wakes up and dies as soon as its parent process (if attached) closes. A suspended thread ignores events. That means any event listeners or timers created inside the thread will not respond to event notifications. Note that threads do not buffer event signals and a suspended thread may miss event signals it was waiting for. For example, if a thread was last waiting on `event.pull("modem_message")` and is "suspended" and a "modem_message" is received by the computer then the thread will miss the event and never know it happened. | + | Suspends (or freezes) a running thread. Returns success and an error message on failure. A "suspended" thread never autonomously wakes up and dies as soon as its parent process (if attached) closes. A suspended thread ignores events. That means any event listeners or timers created inside the thread will not respond to event notifications. Note that threads do not buffer event signals and a suspended thread may miss event signals it was waiting for. For example, if a thread was last waiting on `event.pull("modem_message")` and is "suspended" and a "modem_message" is received by the computer then the thread will miss the event and never know it happened. Please note that if you suspend a thread that is blocked waiting for an event, it is unspecified which event the thread will receive when it is next resumed. |
+ | |||
+ | Suspending the current thread causes the thread to immediately yield and does not resume until `t:resume()` is called explicitly elsewhere. | ||
**Special notes about `t:resume`, `t:suspend`** | **Special notes about `t:resume`, `t:suspend`** | ||
- | Do not think of these methods as `coroutine.resume()` nor `coroutine.yield()`. These methods are indirect and a thread will asynchronously start or stop running on its own. Contrast this to coroutine methods which directly and immediately invoke execution or leave execution of a coroutine. Consider this example: | + | Do not think of these methods as `coroutine.resume()` nor `coroutine.yield()`. These methods are indirect and a thread will asynchronously start or stop running on its own. Contrast this to coroutine methods which directly and immediately invoke execution or leave execution of a coroutine. Consider these examples: |
+ | |||
+ | ```lua | ||
+ | local thread = require("thread") | ||
+ | local t -- this example needs an upvalue to t | ||
+ | t = thread.create(function() | ||
+ | print("start") | ||
+ | thread.current():suspend() | ||
+ | print("after suspend") | ||
+ | os.sleep() | ||
+ | print("after sleep") | ||
+ | end) | ||
+ | ``` | ||
+ | |||
+ | Output: | ||
+ | ``` | ||
+ | start | ||
+ | ``` | ||
```lua | ```lua | ||
Line 137: | Line 162: | ||
t = thread.create(function() | t = thread.create(function() | ||
print("start") | print("start") | ||
- | t:suspend() | + | thread.current():suspend() |
print("after suspend") | print("after suspend") | ||
os.sleep() | os.sleep() | ||
print("after sleep") | print("after sleep") | ||
end) | end) | ||
+ | print("outside thread create") | ||
+ | t:resume() | ||
+ | print("after resume") | ||
``` | ``` | ||
Line 147: | Line 175: | ||
``` | ``` | ||
start | start | ||
+ | outside thread create | ||
+ | after suspend | ||
+ | after resume | ||
+ | after sleep | ||
``` | ``` | ||