lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Thanks for the reference!

I do think there's sense into this, and will do it tomorrow. I was thinking of using [1], [2], ... as the return values, otherwise you seem to have gulped the idea.

function thread()
	for i=1, 10 do lane.return(i) end
end

lo = lane(thread)

while lo:running() do
	print(lo:result())
end

Your code returns values along the way (during processing) whereas I'd simply launch a thread, and pend for the results all at once.

More importantly, the thread script needs to be in a separate chunk than the one being executed (the Lua states are different). So we get something akin:

local thread= [[
   local res=0
   for i=1, 10 do res=res+i end -- something dummy
   return res
]]

lo = lane(thread)

print( lo[1] )	-- or lo.result


David Given kirjoitti 11.2.2007 kello 15.36:

Asko Kauppi wrote:
[...]
I've long wanted to have seeming parallelism in Lua, something akin to
DBUS-kind message passing but made to be syntactically so "sweet" it
would seem native, transparent, and noncluttered in the code. Lately, I
crafted a mechanism that would make such calls seem like regular
function calls, using userdata as handles to the returned values.

[searches on Wikipedia]

Aha. I thought this concept seemed familiar.

http://en.wikipedia.org/wiki/Future_%28programming%29

Guy I knew at university wrote an entire language based around futures (and Linda-based process synchronisation, which is extremely neat but not yet actually *useful*). It worked in much the same way you describe, but had language support so that you didn't need any extra syntax to access the value
of the future.

For Lua, given that we'd need syntactic help anyway, I'd be more inclined to
use actual objects. So, lane(f) creates a new thread object; calling
o:result() (or possibly just accessing o.result) would block until the thread returned a value. It would probably be sensible to allow the thread to return
multiple values, too.

function thread()
	for i=1, 10 do lane.return(i) end
end

lo = lane(thread)

while lo:running() do
	print(lo:result())
end

Hmm. I think we may be reinventing Communicating Sequential Processes...

--
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure out how to
│ use my telephone." --- Bjarne Stroustrup