rust-cookbook icon indicating copy to clipboard operation
rust-cookbook copied to clipboard

Recipe Series: Sharing data between threads

Open AndyGauge opened this issue 7 years ago • 12 comments

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)

AndyGauge avatar Jul 29 '18 03:07 AndyGauge

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

MagnumOpus21 avatar Oct 01 '18 16:10 MagnumOpus21

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

AndyGauge avatar Oct 01 '18 19:10 AndyGauge

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.

AndyGauge avatar Oct 01 '18 19:10 AndyGauge

It makes sense to split this up.

MagnumOpus21 avatar Oct 03 '18 18:10 MagnumOpus21

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

sn99 avatar Oct 04 '18 18:10 sn99

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

j-haj avatar Oct 11 '18 20:10 j-haj

@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.

KodrAus avatar Oct 11 '18 23:10 KodrAus

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. :)

MagnumOpus21 avatar Oct 11 '18 23:10 MagnumOpus21

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.

j-haj avatar Oct 01 '19 10:10 j-haj

how can u determine if you have been hacked

papac25 avatar Aug 19 '20 09:08 papac25

#- - - cschneid

papac25 avatar Aug 19 '20 09:08 papac25

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.

kkibria avatar Feb 26 '23 23:02 kkibria