User Tools

Site Tools


rust:concurrency_resources

Notes on Concurrency and a Resource Collection

Originally, Rust included facilities for message-passing concurrency primitives as part of the language (either in the base language or its standard library). Later the decision was made to make the concurrency primitives more general so that various concurrency models could be supported via 3rd party libraries. The base Rust standard library supports shared-memory parallelism via threads.

Here are some notes on primitive constructs and contributed libraries.

Threads

std::thread

Provides functionality for spawning OS-level (native) threads. In Rust, spawn creates an unnamed, detached thread that has its own stack and local state. Theads can be named and have some data-sharing capabilities.

threadpool::Threadpool

Creates a pool of threads Use methods such as pool.execute to distribute thread fcns across cpus. See the Rust Cookbook for an example of creating a Julia fractal using threadpool here. Or *ack* don't, better to use rayon (see below).

This may be a better tutorial: Multiprocessing-pool-theadpool

std::sync

Provides atomic reference counters (ARC) and access primitives such as mutex, locks, channels Mutex and locks are conceptually the same as in POSIX, but are implemented in a more object-oriented fashion so the borrow-checker can work its resource-management magic → pairing logic goes away! LOCK(); …. UNLOCK(); is a thing of the past! RC and ARC are also implemented this way - each such construct is encapsulated with the data it protects. Channels enforce thread isolation by transferring ownership of data from one to another.

Functional Parallelism

rayon - a crate which provides parallelizing functions paradigms, etc.

More Resources:

Concurrent Data Structures

crossbeam - a crate providing concurrent data structures such as queues/deques

Futures (older model)

futures - notes on this are sketchy…please update! I want to say that futures are like Tasks in Elixir, for kicking off a thread to do a computation while the original thread works on something else, then awaits completion of the future.

Does this crate provide the early version of async I/O, or is that a separate library?

Internet Protocols

tokio - a crate providing “event-driven, non-blocking I/O platform for writing asynchronous applications” (1); includes support for protocols such as TCP, UDP, HTTP, WebSockets and other IPC constructs; composable with Futures

Servers As Futures!

Abstraction of a server as a process that, given a Request, returns a Response, but as a Future. As an example:

fn process(req: Request) -> impl Future<Response> {
   // this is an asynchronous function...
}
   

Async/Await

rust/concurrency_resources.txt · Last modified: 2020/03/04 14:48 by scarl