Giter Club home page Giter Club logo

waitgroup-rs's Introduction

Enables threads to synchronize the beginning or end of some computation.

Examples

use std::thread;

let wg = WaitGroup::new();
let (tx, rx) = mpsc::channel();

for _ in 0..THREADS {
    let wg = wg.clone();
    let tx = tx.clone();

    thread::spawn(move || {
        wg.wait();
        tx.send(()).unwrap();
    });
}

thread::sleep(Duration::from_millis(100));

// At this point, all spawned threads should be blocked, so we shouldn't get anything from the channel.
assert!(rx.try_recv().is_err());

wg.wait();

// Now, the wait group is cleared and we should receive messages.
for _ in 0..THREADS {
    rx.recv().unwrap();
}

Implementations

WaitGroup 对象有一个 count: Mutex<usize> 的域用于记录有多少个 cloned 对象 (含 the original one). WaitGroup 实现了 Drop Trait,当对象 drop 时,count -= 1.

WaitGroup 提供了 wait 方法用于同步线程,调用 wait 时会 drop 当前对象,并当且仅当 count == 0 时返回。

waitgroup-rs's People

Contributors

shuumatsu avatar

Watchers

James Cloos avatar  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.