This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
snoble2022's profile picture

Help with multi-threading: coroutines

Started by snoble2022, 27 December 2012 - 07:32 PM
snoble2022 #1
Posted 27 December 2012 - 08:32 PM
hello, I need help making a coroutine (not parallel, parallel runs slow, plus I don't like it) that I can control (stop thread, new thread, pause thread).

Problems to be solved:
-How do I get threads to switch without calling coroutine.yield in the program
-How do I make a coroutine capable of running like a normal program
-What are the basic coroutine functions and their formats
-And finally, how do I make a coroutine that loads a TaskBar and program at the same time (taskbar is one coroutine and the program is another) with the taskbar updating it self at the same time as the program (Both programs having a while loop, also the reason why I don't want to call coroutine.yeild)
tesla1889 #2
Posted 27 December 2012 - 10:02 PM
only one thread can be live at a time, and coroutine.yield is the only way to do what you're asking for. do something like this:


local c1 = coroutine.create(fn1) -- thread 1
local c2 = coroutine.create(fn2) -- thread 2
local taskbar = function(...)
  <code for taskbar>
end
taskbar = coroutine.create(taskbar)
local event_handler = function(...)
  <code to handle events>
end
while true do
  event_handler(coroutine.resume(c1))
  event_handler(coroutine.resume(taskbar))
  event_handler(coroutine.resume(c2))
  event_handler(coroutine.resume(taskbar))
end