Sergio
Sergio

Reputation: 143

Call Lua from C++ in threads

I have a classifier model in lua to predict the gender of a person in an image, and I need to call it from C++. I'm using the Lua C API.

I have it working for a single prediction, I mean, in C++ I call dofile("script.lua") and the model is loaded from the disk, then I call predict("image1.jpg") and I got the result. So far so good.

Problem

The problem is that I need to be able to call the predict function using threads (several at the same time), because the predict takes a while to return. The model is quite big, so I need to somehow load it once and use it like a singleton.

How can I implement such thing? Or it's not possible / feasible? So far I've tried but none of it worked:

Update

Thanks for the feedback guys. Once it's not possible to implement what I need, I dropped this idea of using the C API to communicate between Lua and C++.

Instead, I'm running a server with Lua - using luasched , and from C++ I'm creating a new connection to the Lua socket each time I have to make a prediction.

Regards, Sérgio

Upvotes: 1

Views: 2547

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473312

What you want is not generally possible with raw Lua. It is an inherently single-threaded system. While you can create multiple independent lua_State objects that can run on independent threads, you cannot call into the same lua_State instance from two different threads.

You could try Lua Lanes, which is ostensibly a threaded framework for Lua. However, it still requires separate lua_States; the main thing it does is (at least in theory) provide a light-weight way to share data between lua_States.

Alternatively, you can create a number of processes equal to the number of cores you have. Each process will have to independently load the model, but you will never load the model more times than the number of cores you want to use. But this way, each process can execute any number of predict calls. You can even take the filename from stdin, thus allowing some master process to farm out work to an individual process.

Upvotes: 2

Related Questions