Jump to content




Coroutines....


  • You cannot reply to this topic
7 replies to this topic

#1 TheJebForge

  • Members
  • 128 posts

Posted 09 January 2015 - 08:43 PM

I'm writing the OS with windows and multitasking. How to run the 2 or more programs at once! I know that uses the coroutines, but how to use the coroutines with program?
I tried that one time, but when i ran it, it errors all of the custom functions... I'm using the public functions.
There's the code:
fil = fs.open("testProgram","r")
text = fil.readLine()
fil.close()
local func,err = loadstring(text)

local con = coroutine.create(func)
while true do
tpr("lool",1,1)
con.resume()
sleep(0.1)
end

tpr(text,x,y) - Print text in specific coordinates

The file with functions is already ran.
It errors all of the custom functions within the coroutine.

Please help with that.

Edited by TheJebForge, 10 January 2015 - 07:58 AM.


#2 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 09 January 2015 - 09:06 PM

I would suggest reading the source for multishell (/rom/programs/advances/multishell) and looking and the parallel API

#3 TheJebForge

  • Members
  • 128 posts

Posted 10 January 2015 - 07:23 AM

I don't understand the multishell code...

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 10 January 2015 - 07:27 AM

What about parallel?

We're here to help you learn, but just giving you code to copy that you don't understand doesn't help you in the long run, and makes more work for us. Try reading through the PIL chapter on coroutines, and check out some related articles on the lua-users wiki. A solid understanding of coroutines is essential to understanding how ComputerCraft's event-based model works.

#5 TheJebForge

  • Members
  • 128 posts

Posted 10 January 2015 - 07:43 AM

I'll try it. The parallel api, for example: parallel.waitForAny(windowDraw, programFunc)
Will that run windowDraw() first and then programFunc()?

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 10 January 2015 - 07:51 AM

More or less. It'll execute windowDraw until the first time that function yields. When that happens, it'll start executing programFunc, then it'll attempt to carry on with windowDraw from where it left off, and so on.

#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 10 January 2015 - 07:52 AM

Depending, of course, on what type of event comes in next and whether or not either or both coroutines passed filters back.

#8 TheJebForge

  • Members
  • 128 posts

Posted 10 January 2015 - 07:58 AM

OK, Thanks! I'll try do something with this.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users