rust-lang-nursery / rust-cookbook

https://rust-lang-nursery.github.io/rust-cookbook
Creative Commons Zero v1.0 Universal
2.29k stars 287 forks source link

Recipe Series: Sharing data between threads #440

Open AndyGauge opened 6 years ago

AndyGauge commented 6 years ago
Crates std, crossbeam_channel
Section 4.3 Sharing data between threads
- shared ownership with `Arc`
- shared stack with `crossbeam`
- mutable data with `Mutex`
- passing messages with `mspc` (crossbeam_channel)
MagnumOpus21 commented 6 years ago

Can I take a shot at this? Any pointers are appreciated.

AndyGauge commented 6 years ago

You don't have to tackle the whole thing, if you want to attempt some of it, we can split it up.

AndyGauge commented 6 years ago

Also, I am not an expert in threading. In fact, I don't know how to recognize idiomatic threaded code. I'm sure we can pull in some other resources for this, but it might take some time.

MagnumOpus21 commented 6 years ago

It makes sense to split this up.

sn99 commented 6 years ago

I could work this one but it might take a 4-5 days

j-haj commented 6 years ago

Just as a heads up for anyone interested in this issue, I am working on a message passing example with crossbeam

KodrAus commented 6 years ago

@j-haj Sounds good!

@MagnumOpus21 A good place to get started is this section of the Rust book. It has a lot of examples of using the std::thread and std::sync modules for concurrency. If you'd like to take a shot at one of the examples, like sharing data between threads with Arc, then we could provide some more support on the PR itself. It doesn't have to be complete when you open it!

crossbeam is a very subtle set of tools, so we'll probably want to keep any examples using crossbeam::epoch directly very simple, or just avoid them altogether in the cookbook, and rely on the higher-level pieces that have already been built over the top.

MagnumOpus21 commented 6 years ago

Sorry, I ghosted for a few days, I had a lot of work to do. I will look into this and will try to open a PR by Sunday. :)

j-haj commented 5 years ago

I have two examples for this: a simple single produce, single consumer example and a more complex example using the so-called ventilator pattern of a single produce to N consumers followed by N producers to a single consumer. I will have the PRs ready in a day or so.

papac25 commented 4 years ago

how can u determine if you have been hacked

papac25 commented 4 years ago

- - - cschneid

kkibria commented 1 year ago

Hi all: One of the problems is to share large data structure shared by many threads. However, data is updated less frequently, it is mostly read access and therefore optimized for reads. Linux employs RCU for similar requirements in its kernel. An example in rust for similar purpose would be useful. Also, an special case would be a single writer thread which can use further optimization. If you folks can put together examples with crossbeam that achieves RCU like data sharing that would be awesome! Just a thought.