Skip to content

Instantly share code, notes, and snippets.

@xxshady
Created April 30, 2024 18:18
Show Gist options
  • Save xxshady/4f76b32b3443eaad338e8448bfd8a9f6 to your computer and use it in GitHub Desktop.
Save xxshady/4f76b32b3443eaad338e8448bfd8a9f6 to your computer and use it in GitHub Desktop.
working tokio multi threading on alt:V
use std::{
cell::RefCell,
sync::{Arc, Mutex},
};
#[altv::main]
fn main() -> impl altv::IntoVoidResult {
let rt = Arc::new(RefCell::new(Some(tokio::runtime::Runtime::new().unwrap())));
let data = Arc::new(Mutex::new(1));
{
let mut data = Some(data.clone());
let rt = rt.clone();
altv::events::on_server_started(move |ctx| {
dbg!(ctx);
let data = data.take().unwrap();
dbg!(std::thread::current());
rt.borrow().as_ref().unwrap().spawn(async move {
dbg!(std::thread::current());
*data.lock().unwrap() = 2;
});
});
}
{
let data = data.clone();
altv::set_timeout(
move || {
altv::log!("data: {:?}", data.lock().unwrap());
},
1000,
);
}
{
let rt = rt.clone();
altv::events::on_resource_stop(move |_| {
rt.take().unwrap().shutdown_background();
});
}
altv::log!("~gl~hello world");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment