6 releases

0.3.4 Jul 7, 2024
0.3.3 Sep 12, 2020
0.3.2 May 17, 2020
0.3.1 Dec 27, 2019
0.1.0 Nov 3, 2018

#237 in Concurrency

Download history 47/week @ 2024-06-25 134/week @ 2024-07-02 242/week @ 2024-07-09 71/week @ 2024-07-16 36/week @ 2024-07-23 42/week @ 2024-07-30 36/week @ 2024-08-06 34/week @ 2024-08-13 52/week @ 2024-08-20 95/week @ 2024-08-27 45/week @ 2024-09-03 42/week @ 2024-09-10 50/week @ 2024-09-17 74/week @ 2024-09-24 112/week @ 2024-10-01 59/week @ 2024-10-08

318 downloads per month
Used in 7 crates (2 directly)

MPL-2.0 license

11KB
109 lines

mpsc_requests

docs.rs badge

For more info, see docs.rs.


lib.rs:

mpsc_requests rewritten for crossbeam, written by @stjepang (https://github.com/crossbeam-rs/crossbeam/issues/353#issuecomment-484013974)

crossbeam_requests is a small library built on top of crossbeam-channel but with the addition of the consumer responding with a message to the producer. Since the producer no longer only produces and the consumer no longer only consumes, the Producer is renamed to [RequestSender] and the Consumer is renamed to [RequestReceiver].

This library is based on crossbeam-requests instead of mpsc channels in the standard library because crossbeam has better performance and better compatibility with android.

A perfect use-case for this library is single-threaded databases which need to be accessed from multiple threads (such as SQLite)

Here's a diagram of the dataflow

|--------------------------------------------------------------------------------------| | Thread | Request thread | Respond thread | Request thread | |--------------------------------------------------------------------------------------| | Struct | RequestSender -> RequestReceiver -> ResponseSender -> ResponseReceiver | | (methods) | (request) -> (poll, poll_loop) -> (respond) -> (collect) | |--------------------------------------------------------------------------------------|

Examples

For more examples, see the examples directory

For even more examples see the tests in the tests directory

Simple echo example

use std::thread;
use mpsc_requests::channel;

type RequestType = String;
type ResponseType = String;
let (requester, responder) = channel::<RequestType, ResponseType>();
thread::spawn(move || {
    responder.poll_loop(|req, res_sender| {
        res_sender.respond(req);
    });
});
let msg = String::from("Hello");
let receiver = requester.request(msg.clone()).unwrap();
let res = receiver.collect().unwrap();
assert_eq!(res, msg);

Dependencies

~345KB