Advertisement

Lua threads problem...

Started by July 20, 2003 10:48 PM
3 comments, last by Illumini 21 years, 4 months ago
Using lua 5, andI just can''t figure this out. My goal is to eventaully havea scripting system where each script has a few functions and all scripts are being proccess simultaneously (well close to ). Anyway while trying to start I wrote the following sample program and script, however it doesn''t work as I expected. Anyone familar with lua please correct me on both my program and comments if they are wrong (they must be)

lua_State *Thread;

int luaWait(lua_State *L)
{
	// yield to test threads

	lua_yield(Thread, 0);
	return 1;
}

void main()
{
	// open lua and std libs

	lua_State *Lua = lua_open();
	lua_baselibopen(Lua);
    lua_iolibopen(Lua);
    lua_strlibopen(Lua);
    lua_mathlibopen(Lua);

	// register dumb function

	lua_register(Lua, "Wait", luaWait);
	
	// create a thread

	Thread = lua_newthread(Lua);

	// parse file in thread

	lua_dofile(Thread, "Test.txt");
	
	// call Main() function

	lua_getglobal(Thread, "Main");
	lua_call(Thread, 0,0);
	lua_pop(Thread, 1);

	// resume yielded thread

	lua_resume(Thread, 0); // should resume where yield left off?	

}
Script looks like this

function Main()
print("In Main")
Wait()
print("Out of Wait")
end
I would expect it to output InMain, then Out of dumb. However the program prints InMain and then terminates. It seems my resume does nothing? I''ve looked at the lua5 documentation and it is next to worthless for making any sense out of this. I just wanna know the right way to run/stop/run script functions from C. I''d greatly apprieciate any help at all. Thanks
The main thread of execution cannot be yielded and resumed. Also, the first time you run a thread, you run it with lua_resume, not lua_call.

How appropriate. You fight like a cow.
Advertisement
Alright, I understand yo ucompletely. But...

Here I create the thread...
Thread = lua_newthread(Lua);

Then I parse the script with that thread
lua_dofile(Thread, "Test.txt");

Now I wanna call my Main function, but I have to call resume first? Alright lets do that...
lua_resume(Thread, 0);

lua_getglobal(Thread, "Main");
lua_call(Thread, 0,0);

Right here the script is executed and Wait is called, wait yield''s Thread, not my main thread (which I thought was the main lua_State* Lua)

lua_pop(Thread, 1);

lua_resume(Thread, 0);

Still nothing after the yield.
No, you misunderstand. "lua_resume" takes the place of "lua_call". You never ever call "lua_call" on a thread. You only ever call "lua_resume". The first time you call "lua_resume", it acts like a "lua_call" (arguments passed in are treated as arguments to the function).

EDIT: oh, and BTW, forget what I said about not calling yield/resume on a main thread... I was incorrectly reading your code.

[edited by - sneftel on July 20, 2003 12:14:49 AM]
Ah alright I'll give that a try. You see I told you I was very confused

EDIT: Wow, Awesome, it works! Thanks a ton man.

[edited by - Illumini on July 20, 2003 12:15:27 AM]

This topic is closed to new replies.

Advertisement