简体   繁体   中英

Can I pass a value from my C host through Lua to a callback C function?

I have Lua embedded in a C host. And I have several C functions registered with Lua.

Is there any way that when I call Lua and then Lua calls my C function, a value can be passed along from the "outer" C code to the "inner" C code?

The specific problem is that I have an HTTP request pointer that I need to access from the callback function, and I'd rather not store it in a global variable due to this potentially being multithreaded code.

When your "outer" C code calls Lua, pass the HTTP request pointer as a light userdata argument. Inside Lua, treat it as an opaque value that you pass to the "inner" C callback as an argument again. Then have your "inner" C code just read it and cast it back to the right type.

By the way, Lua itself isn't thread-safe, so be careful of that if your application is multithreaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM