Code Monkey home page Code Monkey logo

threadpool's Introduction

threadpool

A basic threadpool that uses work-stealing and allows closures with non-static references.

Currently only foreach is implemented. This function takes a mutable slice and closure which mutates an element of the slice, and dispatches this to each thread in the pool on a first-come-first-serve basis. foreach returns once all elements in the slice have been visited.

Example usage:

let mut data: Vec<usize> = (0..65536).collect();

let mut threadpool = ThreadPool::new(1);

let offset: usize = 10;

threadpool.foreach(&mut data, |x| {
    *x = *x + offset; // <---------- 'offset' is borrowed from multiple threads here!
});

assert!(data.iter().enumerate().all(|(i, x)| *x == i + 10));

Is this library safe and sound?

Um, probably? Thread-safe atomics, channels, and barriers are used to handle all synchronization and the included tests run and pass. However, I don't have a rigorous proof that it is guaranteed to be memory safe, and part of the implementation relies on std::mem::transmute to cast away the lifetime bound on the closure being passed to other threads. This is a known-workaround for writing threading primitives as mentioned in item 3 of rust-lang/rust#55043. The use of a shared one-off barrier inside of foreach serves to ensure that all threads have finished using and dropped the closure before foreach returns.

threadpool's People

Contributors

timstr 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.