I've not been able to find an answer to this question although I imagined it should be pretty common, so I'm guessing I'm doing something stupid or didn't read the manual properly.
Anyway, here's what I'm trying to do. I have a program that has a few C functions that are registered for Lua.
at another point, I call the lua function with
lua_getglobal(mainL,"interact");
and
if (lua_pcall(mainL, 2, 0, 0) != 0)
printf("error running function `f': %s",
lua_tostring(L, -1));
printf("interact\n");
Now in the Lua function, I call the other registered C functions a lot. And it seems like every time it does that, it runs in its seperate thread. (Correct me if I'm wrong)
So what I'm trying to ask is if there is anyway for it to block until the C function call is finished before executing the next line in the Lua function.
(Yes, I have tried using mutex in my C program, it works for me, but doesn't seem to work for others on other PC for some reason, so I'm trying to make it blocking since that will make everything a lot easier)
Thank you
1 Answer 1
And it seems like every time it does that, it runs in its seperate thread. (Correct me if I'm wrong)
You're wrong ;-) Or at least, if you are seeing other threads created, then something in the C code you call from Lua is doing that. C called from Lua (and vice-versa) will be explicitly blocking.
-
OMG! For the longest time, I thought it was creating another thread because my text kept overriding the previous one and a mutex solved it~~ But I suppose it was just the wrong positioning of a restore commandStanley Wu– Stanley Wu2012年08月01日 11:40:56 +00:00Commented Aug 1, 2012 at 11:40
-
If you were mixing different buffered I/O schemes for printing debug text then you could also see this behavior. Stock Lua uses
stdio
, andprint
writes tostdout
which will be differently buffered depending on platform conventions and whether it is going to a terminal, pipe, or file. If your C code was writing on raw file descriptors that would interact "oddly", for instance.RBerteig– RBerteig2012年08月01日 23:40:32 +00:00Commented Aug 1, 2012 at 23:40