Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Mutable Variables Between Threads In Rust

Editor's note: This code example is from a version of Rust prior to 1.0 and is not syntactically valid Rust 1.0 code.

Is it possible to share a mutable variable between multiple threads in Rust? Given the following:

fn main() {

    let mut msg = "Hi";
    // ...
    msg = "Hello, World!";

    do spawn {
        println(msg);
    }

    do spawn {
        println(msg);
    }

}

I get this error:

error

The variable just needs to be readonly to the spawned threads. The variable has to be mutable though, because what I'm really trying to do is share a HashMap between multiple threads. As far as I know there is no way to populate a HashMap unless it's mutable. Even if there is a way to do that though, I'm still interested in knowing how to accomplish something like this in general.

Thank you!

like image 380
Evan Byrne Avatar asked Jul 26 '13 17:07

Evan Byrne


People also ask

How do you share data between Rust threads?

There are ways to send data between threads. This is done in Rust using channels. std::sync::mpsc::channel() returns a tuple consisting of the receiver channel and the sender channel. Each thread is passed a copy of the sender with clone , and calls send .

How do I share a variable between two threads?

You should use volatile keyword to keep the variable updated among all threads. Using volatile is yet another way (like synchronized, atomic wrapper) of making class thread safe. Thread safe means that a method or class instance can be used by multiple threads at the same time without any problem. Save this answer.

Can two threads read the same variable?

A race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable.

Do threads share function variables?

Threads share all global variables; the memory space where global variables are stored is shared by all threads (though, as we will see, you have to be very careful about accessing a global variable from multiple threads).

Do threads share same local variables?

Tip: Unlike class and instance field variables, threads cannot share local variables and parameters. The reason: Local variables and parameters allocate on a thread's method-call stack. As a result, each thread receives its own copy of those variables.


1 Answers

The "in general" answer (about keeping a hash table mutable while it's shared) is that it's not directly possible; Rust was specifically designed to forbid the sharing of mutable state in order to avoid certain kinds of bugs and to protect certain kinds of safety (and for other reasons).

However, it is possible to create something much like a shared mutable hash table. Imagine that a single task has a mutable hash table. You can write code so that other tasks can send it messages saying, essentially "Update the hash table so that 5 maps to 18" or "Tell me what 7 maps to".

What does this indirection accomplish? First, by copying values into messages, it's not possible for another task to trample all over memory that you're in the middle of reading (a big safety problem), and second, by writing the code indirectly, it's more clear to the programmer what is and is not an atomic operation; the programmer will know not to expect that two consecutive messages about hash table contents aren't necessarily about the same version of the hash table, an assumption that is perfectly valid (at least in Rust, thanks to some other restrictions) about two consecutive reads from an unshared, mutable, hash table.

like image 79
Paul Stansifer Avatar answered Sep 21 '22 07:09

Paul Stansifer