Is It Safe To Pass A Javascript Callback To An Ffi Function Which Calls It In Another Thread?
I have a C function which takes a callback and invokes it on another thread: void call_in_new_thread(void (*callback)()) { // spawn a new thread and call `callback` in it ... }
Solution 1:
I hacked together a quick demo to test this out. It's using Rust instead of C for the native part, but that should be equivalent as Rust can compile to a normal shared library.
After running the demo, I would answer my own questions like this:
- Yes, it seems to be valid and safe
- The JavaScript callback gets executed in the main thread
- Node-FFI seems to handle the synchronization by pushing the JavaScript callback to a queue which gets popped on the main thread
Post a Comment for "Is It Safe To Pass A Javascript Callback To An Ffi Function Which Calls It In Another Thread?"