Fix threading example for sync.

This commit is contained in:
Stephen Chung 2021-01-25 12:55:10 +08:00
parent e902c74073
commit 2a209b82e9

View File

@ -6,6 +6,12 @@ fn main() {
// Channel: Master -> Script
let (tx_master, rx_script) = std::sync::mpsc::channel();
#[cfg(feature = "sync")]
let (tx_script, rx_script) = (
std::sync::Mutex::new(tx_script),
std::sync::Mutex::new(rx_script),
);
// Spawn thread with Engine
std::thread::spawn(move || {
// Create Engine
@ -13,10 +19,19 @@ fn main() {
// Register API
// Notice that the API functions are blocking
#[cfg(not(feature = "sync"))]
engine
.register_fn("get", move || rx_script.recv().unwrap())
.register_fn("put", move |v: INT| tx_script.send(v).unwrap());
#[cfg(feature = "sync")]
engine
.register_fn("get", move || rx_script.lock().unwrap().recv().unwrap())
.register_fn("put", move |v: INT| {
tx_script.lock().unwrap().send(v).unwrap()
});
// Run script
engine
.consume(