God I'm so bad at rust wtf is a mutex man 😭😭

Reply to this note

Please Login to reply.

Discussion

I’m learning rust right now but I haven’t gotten that far🤣🤣🤣

i’m bad too (idk what rust is)

Programming language

A binary Semaphore. 😅

A security guard that only allows one thread to access whatever is inside of it at a time.

mutes.lock()

There you go. Now no one can reply to this thread until I unlock it

I hear the learning curve for rust is pretty steep. I was thinking about looking into it.

Multi-threading stuff. In C++ too.

That’s not a rust thing! IPC!

Also, no one really knows rust

<'a>&mut fucking shit -> 'a

Fuck kinda syntax is this

It’s ok, just compile it and read the binary; it’s easier

😂😂😂

Wait till you hear about poisoned mutexes 🤮

‘a like a type signature but instead of tracking the type of data it tracks the lifetime. Usually it's good to be explicit with these when writing functions and data types that access references, otherwise you can run into confusing borrow checker error messages.

I built a entire app in rust without needing to use (manually define) lifetimes. I suspect it’s one of rust’s parts that can be learned later (when you need to use it).

Don’t worry. You’ll hate rust until you love it. Get ChatGPT to explain and illustrate by writing code snippets for you.

you don’t need to worry about it if you’re not thinking about the performance of your code. You can usually .clone() things without thinking of what is actually happening under the hood (slow stuff)

That’s true. When you’re learning, you just clone everything. 😀

A Mutex, "Mutual Exclusion", is essentially a protective wrapping around data that allows for safe concurrent access and modification.

Consider a scenario where multiple threads are running and trying to modify some shared data. Mutex locks the data when any thread is modifying it. No other thread can access that data until the mutex is unlocked.

Ok that's a pretty clear explanation ty!

Rust forces you to understand how to build thread safe and memory safe code.

ChatGPT… 😀

…In Rust, a Mutex (short for mutual exclusion) is a concurrency primitive used to protect shared data from simultaneous access by multiple threads. It allows only one thread at a time to access the data it protects, ensuring that no two threads modify the same piece of data at the same time, which could cause data races.

Here is a basic example of a Mutex use in Rust:

```rust

use std::sync::{Arc, Mutex};

use std::thread;

fn main() {

let counter = Arc::new(Mutex::new(0));

let mut handles = vec![];

for _ in 0..10 {

let counter = Arc::clone(&counter);

let handle = thread::spawn(move || {

let mut num = counter.lock().unwrap();

*num += 1;

});

handles.push(handle);

}

for handle in handles {

handle.join().unwrap();

}

println!("Result: {}", *counter.lock().unwrap());

}

```

This program creates a Mutex-protected counter shared among ten threads, each of which increments the counter once. Note that `Arc` is used to enable shared ownership of the Mutex across multiple threads. The `lock` method locks the Mutex and returns a guard, `MutexGuard`, that dereferences into the protected data. The guard also automatically releases the lock when it's dropped.

If a thread can't lock a Mutex because it's already locked by another thread, it'll block until the Mutex is unlocked. The `unwrap()` method is used here to handle potential lock poisoning: if a thread panics while holding the lock, subsequent lock attempts will fail with an error. By calling `unwrap()`, we choose to panic in such cases.

Whatever it is, it can't be as bad as using PHP

Sounds like you need some steel wool.

Mutually exclusive write access to a chunk of memory

I’m still with C and Java

😅