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 [2017/09/07 19:30] payonel [Thread Handle API] |
api:thread [2017/09/07 19:32] payonel [Thread Handle API] |
||
---|---|---|---|
Line 138: | Line 138: | ||
**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 | ```lua | ||
Line 145: | Line 145: | ||
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() | ||
Line 155: | Line 155: | ||
``` | ``` | ||
start | start | ||
+ | ``` | ||
+ | |||
+ | ```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) | ||
+ | print("outside thread create") | ||
+ | t:resume() | ||
+ | print("after resume") | ||
+ | ``` | ||
+ | |||
+ | Output: | ||
+ | ``` | ||
+ | start | ||
+ | outside thread create | ||
+ | after suspend | ||
+ | after resume | ||
+ | after sleep | ||
``` | ``` | ||