Follow

Here I thought I was some rust hotshot but then I need to share a mutable value between threads.

`Arc<Option<Mutex<TunnelManager>>>` is one hell of a type. :P

· · Web · 2 · 1 · 0

`Mutex<Option<TunnelManager>>`

is a lot more reasonable. :P

```
{
let mut tunnels1 = tunnels1.lock().await.unwrap();
tunnels1.open_tunnel(route_id).await?
}
```

Ain't it funky

Coding in rust makes me feel so stupid :P

It seems I need to place the mutex into an arc after all?

@mauve mutexes do tend to need to be placed in arcs to work correctly!

@yosh I think my issue is that I am trying to reuse it both in a closure and later in some other code.

I think I just need to rewrite this using channels and treat the two simulated peers as fully separate peers + have them listen on incoming events generated from a third thread doing the networking.

@yosh I'm just too addicted to mutable references on the heap being trivial in JS >:P

@mauve Yeah the usual way to share data among threads is to wrap it in a `Arc<Mutex<T>>`. If you know that the access is read heavy, use an `RwLock` instead of the `Mutex`.

@dan_ballard I like the feeling tbh. It means that when I get stuff working I've learned some new skills or new ways of thinking I can apply for future problems.

I think the tool I'll use here is to structure what I want like coroutines and leverage channels for the inter-thread messaging.

@mauve nice syntax indeed *sigh* I want to learn rust, but Golang (another language Im a newbie at) seems to be better geared towards my usecases

Sign in to participate in the conversation
Mauvestodon

Escape ship from centralized social media run by Mauve.