Difference between revisions of "Channel:performAtomic"

(Created page)
 
m (Re-set a Channel's contents to only have a single value)
Line 22: Line 22:
 
=== Re-set a Channel's contents to only have a single value ===
 
=== Re-set a Channel's contents to only have a single value ===
 
<source lang="lua">
 
<source lang="lua">
local c = love.thread.getChannel("MyChannel")
 
 
 
local function setChannel(channel, value)
 
local function setChannel(channel, value)
 
     channel:clear()
 
     channel:clear()
Line 29: Line 27:
 
end
 
end
  
 +
local c = love.thread.getChannel("MyChannel")
 
c:performAtomic(setChannel, "hello world")
 
c:performAtomic(setChannel, "hello world")
 
</source>
 
</source>
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::Channel]]
 
* [[parent::Channel]]

Revision as of 04:18, 4 October 2015

Available since LÖVE 0.10.0
This function is not supported in earlier versions.

Executes the specified function atomically with respect to this Channel.

Calling multiple methods in a row on the same Channel is often useful. However if multiple Threads are calling this Channel's methods at the same time, the different calls on each Thread might end up interleaved (e.g. one or more of the second thread's calls may happen in between the first thread's calls.)

This method avoids that issue by making sure the Thread calling the method has exclusive access to the Channel until the specified function has returned.

O.png Long-running or otherwise expensive code should be avoided in the function given to this method.  


Function

Synopsis

ret1, ... = Channel:performAtomic( func, arg1, ... )

Arguments

function func
The function to call, the form of function(channel, arg1, arg2, ...) end. The Channel is passed as the first argument to the function when it is called.
any arg1
Additional arguments that the given function will receive when it is called.
any ...
Additional arguments that the given function will receive when it is called.

Returns

any ret1
The first return value of the given function (if any.)
any ...
Any other return values.

Examples

Re-set a Channel's contents to only have a single value

local function setChannel(channel, value)
    channel:clear()
    channel:push(value)
end

local c = love.thread.getChannel("MyChannel")
c:performAtomic(setChannel, "hello world")

See Also

Other Languages