Page 1 of 3

How do you use threads?

Posted: Sun Nov 07, 2010 12:44 am
by zac352
I just spent the last 1/2 hour googling a way to use io.stdin:read() without completely pausing your program. Coroutines don't work. Anyone know a workaround? :ehem:

Re: You people are all familiar with lua...

Posted: Sun Nov 07, 2010 1:04 am
by Robin
Threads?

But if you're using stdin, you're probably not using LÖVE anyway, right?

Anyway, reading from stdin is a blocking function, for good reasons. To disable the blocking, you need to do some deep, largely unportable magic,

Re: You people are all familiar with lua...

Posted: Sun Nov 07, 2010 1:09 am
by zac352
Robin wrote:Threads?

But if you're using stdin, you're probably not using LÖVE anyway, right?

Anyway, reading from stdin is a blocking function, for good reasons. To disable the blocking, you need to do some deep, largely unportable magic,
Awww... :(

Re: You people are all familiar with lua...

Posted: Sun Nov 07, 2010 1:16 am
by Robin
If you really need it, and you're not using LÖVE, you could consider using Python, which has curses and termios in its standard library.

Re: You people are all familiar with lua...

Posted: Sun Nov 07, 2010 1:21 am
by zac352
Robin wrote:If you really need it, and you're not using LÖVE, you could consider using Python, which has curses and termios in its standard library.
I'm scared of whitespace. I don't see that working.

Re: You people are all familiar with lua...

Posted: Sun Nov 07, 2010 1:28 am
by Robin
Then, my friend, you are doomed.
dooooomed.

Re: You people are all familiar with lua...

Posted: Sun Nov 07, 2010 1:37 am
by zac352
Robin wrote:Then, my friend, you are doomed.
dooooomed.
Can you give me an example of how to use the message system in threads? And do threads share _G?

Edit: No, they don't.

Re: How do you use threads?

Posted: Sun Nov 07, 2010 7:55 am
by Mud
zac352 wrote:I just spent the last 1/2 hour googling a way to use io.stdin:read() without completely pausing your program.
Hmm... I was gonna say that this would be trivial to add via Lua's module system (literally 5 or 6 lines of C code), but Love appears to be statically linked to Lua and the API functions aren't exported, so this isn't really an option. Digging around on this site, this is all I was able to find on the subject (2008):
rude wrote:I made a decision (that can be easily changed) to incorporate Lua into EXE file, not to use it as DLL.
That pretty much kills extending Love dynamically. Which is ironic, because in the same post he says:
rude wrote:Because one of the problems of such kind of engines is that in non-trivial games there is always some non-standard functionality that is missing or too slow to be done in scripts. It would be nice to allow users to extend Love with plug-ins.
If Lua was still in a DLL/so, you could.

Re: You people are all familiar with lua...

Posted: Sun Nov 07, 2010 9:15 am
by Robin
zac352 wrote:Can you give me an example of how to use the message system in threads? And do threads share _G?

Edit: No, they don't.
What, in LÖVE?
Mud wrote:If Lua was still in a DLL/so, you could.
Except that it wouldn't be very portable, and your game wouldn't run in SELÖVE.

Re: How do you use threads?

Posted: Sun Nov 07, 2010 10:10 am
by bartbes
I wonder how you guys fail to see it as easy as it is:

input.lua:

Code: Select all

local t = love.thread.getThread()
repeat
    local s = io.read()
    t:send("input", s)
until t:receive("quit")
main.lua:

Code: Select all

function love.load()
    t = love.thread.newThread("input", "input.lua")
    t:start()
end

function love.update(dt)
    local input = t:receive("input")
    if input then
        --input!
    end
end
That being said, I have no idea why you would want to do this.