{"id":16936471,"url":"https://github.com/jonhoo/hurdles","last_synced_at":"2025-03-17T07:32:31.879Z","repository":{"id":62440342,"uuid":"95713971","full_name":"jonhoo/hurdles","owner":"jonhoo","description":"Rust library providing a counter-based thread barrier","archived":false,"fork":false,"pushed_at":"2018-12-04T18:39:05.000Z","size":64,"stargazers_count":53,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-14T20:57:09.497Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonhoo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-28T21:39:44.000Z","updated_at":"2024-07-08T03:36:32.000Z","dependencies_parsed_at":"2022-11-01T21:53:19.432Z","dependency_job_id":null,"html_url":"https://github.com/jonhoo/hurdles","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonhoo%2Fhurdles","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonhoo%2Fhurdles/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonhoo%2Fhurdles/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonhoo%2Fhurdles/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonhoo","download_url":"https://codeload.github.com/jonhoo/hurdles/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221674292,"owners_count":16861814,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T20:57:03.359Z","updated_at":"2024-10-27T12:16:08.416Z","avatar_url":"https://github.com/jonhoo.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hurdles\n\n[![Crates.io](https://img.shields.io/crates/v/hurdles.svg)](https://crates.io/crates/hurdles)\n[![Documentation](https://docs.rs/hurdles/badge.svg)](https://docs.rs/hurdles/)\n[![Build Status](https://travis-ci.org/jonhoo/arccstr.svg?branch=master)](https://travis-ci.org/jonhoo/arccstr)\n\nA scalable barrier (like [`std::sync::Barrier`]) that enables multiple threads to synchronize\nthe beginning of some computation.\n\nThis crate provides a similar interface as [`std::sync::Barrier`], but behaves much better in\nthe face of many concurrently waiting threads, and incurs a lower per-thread latency penalty\n(see benchmarks below). The interface does differ from the standard library barrier however:\n\n - `Barrier` in this crate is `Clone`, and should *not* be wrapped in a `sync::Arc`.\n - `Barrier::wait` in this crate takes a `\u0026mut self` receiver as each thread must keep some\n   local state.\n\nFurthermore, when a thread blocks on `Barrier::wait`, the thread will (currently) *never* be\nsuspended, and instead spin on the barrier. For the first few spins, it will also not call\n`sched_yield` to avoid the cost of thread sleep/wakeup. If threads are expected to reach the\nbarrier at nearly the same time, or barrier latency is critical, this is probably what you\nwant. However, if barriers are staggered and far between, then you may want to use\n[`std::sync::Barrier`] instead, as it is better about handling long waits.\n\n## Examples\n\n```rust\nuse hurdles::Barrier;\nuse std::thread;\n\nlet mut handles = Vec::with_capacity(10);\nlet mut barrier = Barrier::new(10);\nfor _ in 0..10 {\n    let mut c = barrier.clone();\n    // The same messages will be printed together.\n    // You will NOT see any interleaving.\n    handles.push(thread::spawn(move|| {\n        println!(\"before wait\");\n        c.wait();\n        println!(\"after wait\");\n    }));\n}\n// Wait for other threads to finish.\nfor handle in handles {\n    handle.join().unwrap();\n}\n```\n\n## Implementation\n\nAt the time of writing, the implementation of `std::sync::Barrier` internally uses a `Mutex`,\nwhich causes contention with many waiting threads, and incurs an undue performance overhead for\neach call to `wait`.\n\nThis crate instead implements a counter-based linear barrier as described in \"3.1 Centralized\nBarriers\" in Mellor-Crummey and Scott’s paper [Algorithms for scalable synchronization on\nshared-memory multiprocessors][1] from 1991. For a higher-level explanation, see Lars-Dominik\nBraun's [Introduction to barrier algorithms][2].\n\n## Numbers\n\nModern laptop with 2-core (4HT) Intel Core i7-5600U @ 2.60GHz:\n\n```text\ntest tests::ours_2 ... bench:         190 ns/iter (+/- 24)\ntest tests::std_2  ... bench:       2,054 ns/iter (+/- 822)\ntest tests::ours_4 ... bench:         236 ns/iter (+/- 2)\ntest tests::std_4  ... bench:      11,913 ns/iter (+/- 60)\n```\n\nDell server with 2x 10-core (20HT) Intel Xeon E5-2660 v3 @ 2.60GHz across two NUMA nodes:\n\n```text\ntest tests::ours_4  ... bench:         689 ns/iter (+/- 9)\ntest tests::std_4   ... bench:       4,762 ns/iter (+/- 151)\ntest tests::ours_8  ... bench:       1,380 ns/iter (+/- 13)\ntest tests::std_8   ... bench:      17,545 ns/iter (+/- 288)\ntest tests::ours_16 ... bench:       2,970 ns/iter (+/- 33)\ntest tests::std_16  ... bench:      38,215 ns/iter (+/- 469)\ntest tests::ours_32 ... bench:       3,838 ns/iter (+/- 129)\ntest tests::std_32  ... bench:      94,266 ns/iter (+/- 12,243)\n```\n\n[1]: https://dl.acm.org/citation.cfm?doid=103727.103729\n[2]: https://6xq.net/barrier-intro/\n[`std::sync::Barrier`]: https://doc.rust-lang.org/std/sync/struct.Barrier.html\n\nOr, in plot form:\n\n![Barrier time as the number of threads grow](perf.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonhoo%2Fhurdles","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonhoo%2Fhurdles","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonhoo%2Fhurdles/lists"}