Difference between revisions of "Parallel.waitForAll"
From ComputerCraft Wiki
(Function -> Function (type)) |
Bomb Bloke (Talk | contribs) (Some attempts at clarification.) |
||
Line 5: | Line 5: | ||
|api=parallel | |api=parallel | ||
|addon=ComputerCraft | |addon=ComputerCraft | ||
− | |desc=Lets you run multiple functions at the same time. | + | |desc=Lets you run multiple functions at the same time, alternating between them until all have finished. Note that it takes ''function names'' as parameters (eg "function1", "function2", etc), as opposed to ''function calls'' (eg "function1()", "function2()", etc).<br><br> |
+ | |||
+ | The functions are not actually executed simultaniously, but rather this API will automatically switch between them whenever they yield (eg whenever they call [[Coroutine.yield|coroutine.yield()]], or functions that call that - eg [[Os.pullEvent|os.pullEvent()]] - or functions that call ''that'', etc - basically, anything that causes the function to "pause").<br><br> | ||
+ | |||
+ | Each function executed in "parallel" gets its own copy of the event queue, and so "event consuming" functions (again, most anything that causes the script to pause - eg [[Os.sleep|sleep()]], [[Rednet.receive|rednet.receive()]], most of the [[Turtle_(API)|turtle API]], etc) can safely be used in one without affecting the event queue accessed by the other. | ||
|examples= | |examples= | ||
{{Example | {{Example | ||
− | |desc=Tells the computer to run function1 and function2 at the same time. Will broadcast what the | + | |desc=Tells the computer to run function1 and function2 at the same time. Will broadcast what the user enters and prints what it receives over rednet. It will stop when it has received a message ''and'' the user has entered something. |
− | |code=function1 = [[Function (type)|function]]() | + | |code=function1 = [[Function (type)|function]]() -- Define function1. |
[[print]] ([[rednet.receive]]()) | [[print]] ([[rednet.receive]]()) | ||
end | end | ||
− | + | ||
+ | [[Function (type)|function]] function2() -- Define function2. | ||
[[rednet.broadcast]] ([[read]]()) | [[rednet.broadcast]] ([[read]]()) | ||
end | end | ||
− | parallel. | + | |
+ | parallel.waitForAny (function1, function2) -- Run both functions in parallel until both complete. | ||
|output=Prints what it received via rednet and broadcasts the message the user typed. | |output=Prints what it received via rednet and broadcasts the message the user typed. | ||
}} | }} |
Revision as of 06:26, 12 February 2014
Function parallel.waitForAll | |
Lets you run multiple functions at the same time, alternating between them until all have finished. Note that it takes function names as parameters (eg "function1", "function2", etc), as opposed to function calls (eg "function1()", "function2()", etc). The functions are not actually executed simultaniously, but rather this API will automatically switch between them whenever they yield (eg whenever they call coroutine.yield(), or functions that call that - eg os.pullEvent() - or functions that call that, etc - basically, anything that causes the function to "pause"). | |
Syntax | parallel.waitForAll(function1, function2, so on) |
Returns | ? |
Part of | ComputerCraft |
API | parallel |
Examples
Example | |
Tells the computer to run function1 and function2 at the same time. Will broadcast what the user enters and prints what it receives over rednet. It will stop when it has received a message and the user has entered something. | |
Code |
function1 = function() -- Define function1. print (rednet.receive()) end function function2() -- Define function2. rednet.broadcast (read()) end parallel.waitForAny (function1, function2) -- Run both functions in parallel until both complete. |
Output | Prints what it received via rednet and broadcasts the message the user typed. |