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

parallel2 - a better parallel API - fake events, pause and resume threads, and more!

Started by lfairy, 15 September 2012 - 07:46 AM
lfairy #1
Posted 15 September 2012 - 09:46 AM
parallel2 is a complete rewrite of the ComputerCraft parallel API. It gives you many features over the original, including:
  • Starting and stopping a thread from another one
  • Stepping threads manually
  • Sending fake events
Download - Documentation - Source (GitHub)

Versions
2
  • Clean up the coroutine handling code – it's much easier to read now
  • Add a limit parameter to run()
1
  • Add fork()
  • Move documentation to a separate file
0
  • Initial release
chiloxsan #2
Posted 15 September 2012 - 10:19 AM
Looks very nice! This will be very helpful when trying to write an operating system with modern multitasking techniques.
Cyclonit #3
Posted 15 September 2012 - 09:17 PM
First of all: Nice work. I already changed all of my parallel uses to use your API instead and have several further uses in my mind.

However it would be cool if you could help me with a small problem I have or at least tell me whether doing this is possible with your API.
I have a function ("runClock()") which is supposed to run every time and print a little clock to the upper right corner of the screen. Currently I don't have a clue how I can make it, that the runClock() function allows other functions to run while it is holding in between the updates. Neither setting an alarm every second and waiting for it to occur makes simultaneously running functions start nor does os.sleep().

This is the code I'm currently using to find an answer:


function runClock(mon)
    while (true) do
	    cm.printAligned(mon, "right", "top", textutils.formatTime(os.time(), true) .. " <")
	    os.sleep(0.2)
    end
end
function printKey(mon)
    local event, p1, p2, p3, p4, p5, p6 = os.pullEvent()
   
    if (event == "key") then
	    print("Key: ", p1)
    end
end
lfairy #4
Posted 16 September 2012 - 01:47 AM
Cyclonit:

For the code you posted, you can run both functions in parallel like so:


local m = -- get a monitor somehow

parallel2.group(function () runClock(m) end,
                function () printKey(m) end):run()

The trick is to wrap them both in anonymous functions – this way, they don't start running until parallel2 tells them to.