Coroutines

I’ve been messing around with GLua for a good few years now and have never found the need to use coroutines. A lot of people probably don’t even know what they are, seeing as they’re used close to no-where.

Lua coroutines are like Threads, though they don’t run concurrently, they run collaboratively. So if one of them freezes up, so does your game.

I don’t know about other people but one of my main uses of threads in other languages such as C# and Java is stop certain blocks of code slowing down or freezing up the main Thread. This functionality isn’t achievable in Gmod and it would be hella useful if it was. I’m pretty sure Computercraft, a mod for Minecraft runs coroutines concurrently. If it does, running coroutines concurrently on GMod’s implementation of the Lua 5.1 engine isn’t out of reach.

Lua has no native multithreading support and I doubt Garry wants to faff about with mutex locking all the things

Also coroutines have been broken since at least the GM13 beta and haven’t been fixed yet, probably because they aren’t used often

They seemed to work when I tested them just now and of course Garry doesn’t want to try anything difficult, that would just be too difficult.

I thought C functions were broken. Hrm. Color me mistaken, last time I checked they’d spit out awful, blasphemous insults to reality whenever you tried to print something.

How much did you test?

Pretty thoroughly, printing seems to work too :



] lua_run COCKDICKS = coroutine.create(function() print(PENISFACE) end)
> COCKDICKS = coroutine.create(function() print(PENISFACE) end)...
] lua_run PENISFACE = "hello"
> PENISFACE = "hello"...
] lua_run coroutine.resume(COCKDICKS)
> coroutine.resume(COCKDICKS)...
hello


This and other basic things that are said to be inside the limitations of coroutines seem to work.

Coroutines can basically do anything, except some debug stuff because it’s really likely to be used, generally, coroutines are good letting a function yield, eg. pause the function execution and continue at some other time, this can help to turn time consuming iterative calculations into step-wise calculation simply by yielding every x-loops.

There are a ton of other examples like iterators and such.

Well smack my neighbor and call me Pedro, I guess he fixed them.

maybe you were originally mistaken, Pedro?

Take it back. Coroutines are still fucked, just in more subtle ways. I think.

I’ll make a thread in Developments when I figure out what the fuck is going on.

e: okay apparently SOMETIMES when you call coroutine.resume() on a thread, it says “expected string, got thread”.

What the shit?

[editline]12th February 2013[/editline]

GOTCHA

If you use the net library inside a coroutine, you break the thread and it throws bizarre errors when it’s resumed.

[lua]function broken()
print(“starting broken coroutine”)
net.Start(“Message”)
net.WriteString(“lol”)
net.WriteUInt(2, 4)
net.Broadcast()

coroutine.yield()
print("BORK")

end

local bork = coroutine.create(broken)
coroutine.resume(broken)
[/lua]