-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoroutines.lua
More file actions
80 lines (63 loc) · 3.7 KB
/
Copy pathcoroutines.lua
File metadata and controls
80 lines (63 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
In Lua, you can use coroutines to achieve a form of multithreading. However, it's important to note that Lua's coroutines are not threads in the traditional sense. They are cooperative, which means they only yield control at specified points in the program, unlike threads in many other languages, which can be preemptively switched out by the operating system.
Here's a basic example of how you might use coroutines and the LuaSocket library to perform multiple tasks at once:
```lua
local socket = require("socket")
-- Create a coroutine that performs a task
function task(name, time)
local start = socket.gettime()
while true do
if socket.gettime() - start >= time then
print(name .. " finished task")
coroutine.yield(false) -- Task is done
else
coroutine.yield(true) -- Task is not done
end
end
end
-- Create two tasks
local task1 = coroutine.create(task)
local task2 = coroutine.create(task)
-- Start the tasks
coroutine.resume(task1, "Task 1", 2)
coroutine.resume(task2, "Task 2", 3)
-- Run until all tasks are done
while coroutine.status(task1) ~= "dead" or coroutine.status(task2) ~= "dead" do
if coroutine.status(task1) ~= "dead" then
local task1Status = coroutine.resume(task1)
if not task1Status then
print("Task 1 is done")
end
end
if coroutine.status(task2) ~= "dead" then
local task2Status = coroutine.resume(task2)
if not task2Status then
print("Task 2 is done")
end
end
socket.sleep(0.1) -- Avoid busy waiting
end
```
In this example, we create two tasks that each run for a specified amount of time. We use the `coroutine` and `socket` libraries to manage these tasks and check if they're done. When a task is done, it yields `false`, and we print a message to indicate that it's done.
Please note that this is a very basic example and real-world usage would require more complex handling, especially for error situations.
In Lua, you can use coroutines to achieve concurrency. Coroutines in Lua are similar to threads (in the sense of multithreading): they are independent lines of execution that can be suspended and resumed. However, unlike threads, coroutines only yield explicitly, not implicitly due to blocking operations or preemption.
Here's a simple example of how you can use coroutines to simulate concurrency:
```lua
-- Define a function that will be used as a coroutine
function myCoroutine(name, n)
for i = 1, n do
print(name, i)
coroutine.yield() -- This suspends the coroutine
end
end
-- Create two coroutines
co1 = coroutine.create(myCoroutine)
co2 = coroutine.create(myCoroutine)
-- Run the coroutines in a way that simulates concurrency
for i = 1, 10 do
coroutine.resume(co1, "Coroutine 1", 10)
coroutine.resume(co2, "Coroutine 2", 10)
end
```
In this example, `myCoroutine` is a function that takes a name and a number `n`, and prints the name and a counter `i` `n` times. After each print, it yields control back to the main thread, which then resumes the other coroutine.
The main thread creates two coroutines, `co1` and `co2`, and then enters a loop where it resumes each coroutine in turn. This gives the illusion of the two coroutines running concurrently, even though they're actually running in an interleaved fashion on a single thread.
Please note that Lua's coroutines are not parallel, they are concurrent. This means that they don't run at the same time, but they can be paused and resumed to give the illusion of parallelism. If you need actual parallelism, you would need to use threads or processes, which are not supported natively in Lua, but can be achieved using libraries like Lua Lanes or LuaJIT's FFI interface.