I’m pretty new to Rust so forgive me.
I’d like to make a library using Rust, and I’d like that library to have an init() function which spawns a thread, and for subsequent function calls to send messages to that thread.
I also want this library to be accessible from C code.
My question is: what would be the best way to go about doing that?
If I was writing the library in C or C++, I’d create global variables to handle the synchronization between my library’s personal thread and the calling thread. But, it appears that creating a global that’s accessible across threads in Rust is an awkward process typically involving 'unsafe’s in my code.
So what about returning a reference to a data structure which contains the stuff needed to send messages to the thread, and requiring subsequent calls to pass in this structure? Is that a good idea? Would it work from a C program without becoming unwieldy? If this is something reasonable, can someone give me an example of how I could write it?
For background, I’m making a library for a certain type of network server which will feed data to its clients in real time, sending constant synchronization signals. These signals need to be sent whether the main program has any new information or not. I don’t want the user of my library to worry about calling a function every X milliseconds to ensure the packets are sent on time, so a separate thread seems like the answer. I want to spare the user of my library the business of creating a thread himself, as well, especially in C where it can be an uncomfortable process with portability concerns.
Maybe my head is in completely the wrong place, or I’m making some poor assumptions? I’d be interested to hear suggestions.