In my sample application, I have basically two threads.
The main thread contains a Lua engine (which is not thread-safe) and registers some C++ functions into this engine. However, one of these functions takes too long to perform (since it downloads some file over the internet) and I want the Lua engine to continue doing other stuff without blocking during the download process.
Therefore, I want to make it asynchronous: When the downloadFile() function is called from Lua, I create a new thread which performs the download. Then, the function returns and the Lua engine can process other work. When the download is finished, the second thread somehow needs to tell the main thread that it should somehow call some additional function processFile() to complete it.
This is where I’m struggling now: What is the easiest / cleanest solution to achieve this?
A new user-data object can be returned by your
downloadFile()or similarly named function for kicking off the thread. This new user-data object would contain the thread handle and have an associated meta-table with an__indexentry that would have a function for checking the completion status of the download, and contain other synchronization functions.Possibly looking like this:
or this: