lua-users home
lua-l archive

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


lua-l searchable is down, so I must post...(sorry for length!)

I have a situation in which a C program calls a lua function, and the lua function requests (from the C program) that yet another (or more) lua scripts be executed:

from C: executeLuaString("luaFn1()"); // creates thread, loads buffer, calls resume

-- lua program follows
-- store the 'lightuserdata' value of a C ScriptObject* so we can kill it later.
gScriptObjectToKill = nil

function luaFn1()
	-- call C to create a new 'thread' and 'resume' it
	gScriptObjectToKill = Capi_executeLuaString("luaFn2()")
	-- perhaps launch a number of other 'processes' here also
end

function luaFn2()
	while true do
		-- do some stuff
		processStep1()

		-- wait 5 seconds by having C host yield us and resume us 5 seconds later
		waitSeconds(5)

		-- do more stuff, wait more, etc...
	end
end

function userSaysHalt
	-- we want to stop luaFn2, which is still running.
	if gScriptObjectToKill ~= nill then
Capi_killScript( gScriptObjectToKill ) -- luaFn2 will not be resumed anymore
	end
end


So, is there anything fishy about this construction? the Capi_killScript just deletes the C object - it doesn't free the thread in any way (there is no way to free the thread created with lua_newthread). But luaFn2 will never finish - is this OK? There doesn't appear to be a way to make it stop NOW (we must wait for it to yield) Can it be garbage collected if it never completes?

Still fairly new to lua...

thanks,
thomas blom