0

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

Roddy
68.4k45 gold badges172 silver badges281 bronze badges
asked Aug 1, 2012 at 11:12

1 Answer 1

2

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.

answered Aug 1, 2012 at 11:33
2
  • 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 command Commented 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, and print writes to stdout 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. Commented Aug 1, 2012 at 23:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.