microsoft / Chakra-Samples

Repository for Chakra JavaScript engine related samples.
MIT License
216 stars 84 forks source link

Multi-thread code samples request #78

Open commonsuppliz opened 7 years ago

commonsuppliz commented 7 years ago

I need to access chakraCore in different thread out of owner context thread (ex. background). Current existing sample in C# is just in one thread. I need sample code to work in multi-thread chakraCore.

liminzhu commented 7 years ago

Handling multiple threads would be an interesting topic. It's important to understand that a runtime can only live on one thread at a time (see rental-threading). So in order to switch a runtime from thread A to thread B, you need to give up the runtime on thread A,

// on thread A JsSetCurrentContext(nullptr);

// on thread B JsSetCurrentContext(context1);

commonsuppliz commented 7 years ago

Do You means generated JS Objects wll adhere to one thread (Context), and can not pass across the thread?

liminzhu commented 7 years ago

No. All your JS objects are adhered to the context they are created on, and you can move context between threads.

liminzhu commented 7 years ago

https://github.com/Microsoft/ChakraCore/wiki/JavaScript-Runtime-%28JSRT%29-Overview#concepts should give you a good idea about what runtime and context are.

commonsuppliz commented 7 years ago

I understand "and you can move context between threads.", and I beg to provide C# code to demonstrate in C# to rental thread context switching.

liminzhu commented 7 years ago

Is there a particular scenario that you're after? I believe the general idea regardless of language is in https://github.com/Microsoft/Chakra-Samples/issues/78#issuecomment-297580357.