Is it safe to pass a JavaScript callback to an FFI function which calls it in another thread?

后端 未结 1 397
悲哀的现实
悲哀的现实 2021-01-05 09:50

I have a C function which takes a callback and invokes it on another thread:

void call_in_new_thread(void (*callback)())         


        
相关标签:
1条回答
  • 2021-01-05 10:32

    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
    0 讨论(0)
提交回复
热议问题