Coder Social home page Coder Social logo

conditional-variable-rust-example's Introduction

Conditional Variable in Rust

A condition variable can block a thread without wasting CPU time while waiting for a specific condition to be fulfilled.

https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html

For example, a consumer thread must wait on the condition of there being items to consume.

If that didn't help, here's a better example that uses producers and consumers but with real-world objects.

Crab Cook Example

Let's say you're trying to cook some crabs! ๐Ÿฆ€

You have one thread prepare the crabs for cooking. You have another thread actually cook the crab.

In this case, the preparing thread is the producer. The cooking thread is the consumer.

The cooking thread has a pot that can contain 3 crabs. It is most cost efficient to cook 3 crabs at the same time, so your cooking thread will wait for there to be 3 crabs prepared before attempting to cook any.

3 crabs being prepared is the condiiton to wait for! So take a look at how main.rs implements these condition variables.

Optimizations

The crab cooking is just an example. A friendly user by the name of trentj pointed out that this is closer to a multi-producer-single-consumer problem:

https://doc.rust-lang.org/std/sync/mpsc/

conditional-variable-rust-example's People

Contributors

jboetticher avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.