Channel (programming): Difference between revisions

Content deleted Content added
No edit summary
Added Love2D as it is an open source library for Lua that uses channels and uses familiar data structures (stacks) in its channel implementation to simplify its usage.
Line 19:
 
== Examples ==
 
=== Lua Love2D ===
The [[Löve (game engine)|Love2D]] library which is part of the [[Lua (programming language)|Lua]] programming language implements channels with push and pop operations similar to stacks. The pop operation will block so as long as there is data resident on the stack. A demand operation is equivalent to pop, except it will block until there is data on the stack<syntaxhighlight lang="lua">
-- A string containing code which will be intereprted by a function such as loadstring(),
-- but on the C side to start a native thread.
 
local threadCode = [[
love.thread.getChannel("test"):push("Hello world!")
]]
 
 
function love.load()
-- Start the thread.
thread = love.thread.newThread(threadCode)
thread:start()
-- The thread will block until "Hello world!" is popped off channel test's stack.
-- Because the channel can be popped from before the thread first executes, there may not be data on the stack.
-- in that case use :demand() instead of :pop() because :demand() will block until there is data on the stack and then return the data.
print(love.thread.getChannel("test"):demand())
-- The thread can now finish.
end
 
</syntaxhighlight>
 
=== XMOS XC ===