Starting to learn Arc and Mutex | Rust Language

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ก.พ. 2024
  • Arc and Mutex - part 1 - safely share ownership of data across threads by incrementing the reference count. Just to recap: Each thread has it's own stack, but heap memory is
    shared across all threads of a process.
    🟢 The Arc type, short for "atomic reference counting," is a thread-safe mechanism for managing shared ownership of data.
    🟢 Arc creates another pointer to the same allocation, increasing the strong reference count.
    🟢 It allows multiple references to the same piece of data while keeping track of the number of references.
    🟢 In situations where concurrent writes are possible, introducing a Mutex (short for mutual exclusion) is a good choice. (Arc doesn't inherently provide protection against data races when multiple threads are modifying shared data concurrently).
    🟢 This makes it suitable for concurrent (multi-threaded) programming.
    🟢 The Arc type is an alternative to Rc, additionally providing atomic operations to ensure safe sharing of data across threads.
    🟢 When using Arc, it's important to ensure that the underlying data is designed to be immutable if you want to guarantee immutability in your program. This ensures that multiple threads can safely share and read the data without causing data races.
    • Concurrency
    To get the thread id use
    🟢 thread::current().id()
    - An Anlogy for Arc : 1 person with a group ticket getting all of their friends into a nightclub -
    ---------------------------------------------------------------------------------------------------------------------------
    1. *Group Arrival (Creating a Group Arc):* Picture one person arriving at the nightclub entrance with a group of friends. This person represents the initial `Arc` reference, and the group collectively shares a single ticket, which we'll call the "group" ticket. In Rust terms, this person represents the original `Arc`, and the "group" ticket represents the shared ownership of data.
    2. *Presenting a "Group" Ticket (Cloning the Original Arc):* The person at the entrance presents the "group" ticket to the doorman, signaling that they want their friends (threads) to join the party. In Rust, this is akin to invoking `Arc::clone` on the original `Arc`. This action effectively creates new references (individual tickets) for each friend (thread) that wants to join.
    3. *Doorman (Arc) Issuing Individual Tickets (Cloned Arcs):* The doorman (representing `Arc`) takes note of the request and starts issuing individual tickets (cloned `Arc` references) to each friend (thread) from the "group" ticket. Each friend receives their own ticket (reference) to access the shared data independently.
    4. *Keeping a Note of Issued Tickets (Reference Counting):* The doorman keeps track of how many tickets (references) have been issued from the "group" ticket. This mirrors the reference counting mechanism in Rust, where the reference count increases when a new `Arc` is cloned and decreases when an `Arc` is dropped.
    This refined analogy effectively captures the concept of cloning an `Arc` to create new references, demonstrating how one initial `Arc` can be used to share ownership among multiple threads in a controlled and synchronized manner. The "group" ticket metaphor adds a visual and intuitive aspect to understanding the shared ownership of data in a concurrent context.
    Code :
    🟩 gist.github.com/RGGH/57dc74c8...
    Rustnomicon :
    🟩 doc.rust-lang.org/nomicon/arc...
    Rust Docs:
    🟩 doc.rust-lang.org/std/sync/st...
    Bonus Example:
    🟩 Gist: gist.github.com/rust-play/4f5...
    Linux Hosting - VPS server - Webdock
    ----------------------------------------------------------------
    🟩 Webdock Linux Hosting : webdock.io/en?maff=wdaff--170
    Nostr
    ---------
    @RngWeb
    findthatbit@iris.to
    findthatbit.info/
    redandgreen.co.uk/
    #rustlang #multithreading #Arc #Mutex

ความคิดเห็น • 4

  • @Michaeljamieson10
    @Michaeljamieson10 3 หลายเดือนก่อน +2

    awesome

  • @flyingbluelion
    @flyingbluelion 3 หลายเดือนก่อน +1

    ask the free version of GPT about Rust syntax and you may learn something, but be careful as you can confuse it and it will then lie to you with great confidence. Ask it how to do something you know is easy but you cannot remember the syntax and it will be better than you reading the std library. Including Arc Mutex and channels.

    • @learning_rust
      @learning_rust  3 หลายเดือนก่อน

      Exactly. Don't trust, verify - and if it ever offers a fix for a piece code, I ask it to explain itself and then I go and research it plus check the relevant Rust documentation. I do wonder how deeply it was trained on Rust code as it can offer similar suggestions even when you rephrase a question by a significant amount. 👍