Rust concurrency: five easy pieces.

First

https://github.com/gterzian/rust_five_easy_pieces/blob/abcffcc72915d2118bdbe5ff1311fad3d3abc74b/src/main.rs#L13
  1. wakes-up when a message is received,
  2. handles the incoming message, and
  3. goes back to blocking on the channel.
  1. the exit confirmation should come as last.

Second

https://github.com/gterzian/rust_five_easy_pieces/blob/abcffcc72915d2118bdbe5ff1311fad3d3abc74b/src/main.rs#L78
  1. cannot know if the parallel component will exit after the work has been performed.
  1. all work performed by workers has finished.

Third

https://github.com/gterzian/rust_five_easy_pieces/blob/abcffcc72915d2118bdbe5ff1311fad3d3abc74b/src/main.rs#L138
  1. The parallel component also owns a counter of ongoing work.
  2. Workers on the thread-pool have access to a new channel, which they use to signal to the parallel component when they have completed a unit of work.
  3. The parallel component now selects over two channels: the one for messages from the main component, and the one for messages from workers on the thread-pool.

Fourth

https://github.com/gterzian/rust_five_easy_pieces/blob/763ffe0b4756de1ccde2793491c6d49b36a4c234/src/main.rs#L237

Fifth

https://github.com/gterzian/rust_five_easy_pieces/blob/763ffe0b4756de1ccde2793491c6d49b36a4c234/src/main.rs#L358

Further reading

These examples are based on real-world code found in Servo’s net component, in particular how the CoreResourceManager spawns parallel fetch workers on a thread-pool, and how those parallel fetches share an HTTP cache and a corresponding cache state. Please take a look.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store