{"id":13730176,"url":"https://github.com/rigtorp/MPMCQueue","last_synced_at":"2025-05-08T02:31:30.868Z","repository":{"id":41247911,"uuid":"54938037","full_name":"rigtorp/MPMCQueue","owner":"rigtorp","description":"A bounded multi-producer multi-consumer concurrent queue written in C++11","archived":false,"fork":false,"pushed_at":"2024-03-08T11:12:31.000Z","size":85,"stargazers_count":1195,"open_issues_count":9,"forks_count":163,"subscribers_count":45,"default_branch":"master","last_synced_at":"2024-11-11T22:40:53.353Z","etag":null,"topics":["concurrency","cpp","cpp11","header-only","queue"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rigtorp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-29T02:04:50.000Z","updated_at":"2024-11-11T06:29:21.000Z","dependencies_parsed_at":"2022-07-12T23:10:34.363Z","dependency_job_id":null,"html_url":"https://github.com/rigtorp/MPMCQueue","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2FMPMCQueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2FMPMCQueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2FMPMCQueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2FMPMCQueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rigtorp","download_url":"https://codeload.github.com/rigtorp/MPMCQueue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224689428,"owners_count":17353375,"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":["concurrency","cpp","cpp11","header-only","queue"],"created_at":"2024-08-03T02:01:11.007Z","updated_at":"2024-11-14T20:31:57.391Z","avatar_url":"https://github.com/rigtorp.png","language":"C++","readme":"# MPMCQueue.h\n\n![C/C++ CI](https://github.com/rigtorp/MPMCQueue/workflows/C/C++%20CI/badge.svg)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/rigtorp/MPMCQueue/master/LICENSE)\n\nA bounded multi-producer multi-consumer concurrent queue written in C++11.\n\nIt's battle hardened and used daily in production:\n- In the [Frostbite game engine](https://www.ea.com/frostbite) developed by\n  [Electronic Arts](https://www.ea.com/) for the following games:\n  - [Anthem (2019)](https://www.ea.com/games/anthem)\n  - [Battlefield V (2018)](https://www.ea.com/games/battlefield/battlefield-5)\n  - [FIFA 18 (2017)](https://www.easports.com/fifa/fifa-18-cristiano-ronaldo)\n  - [Madden NFL 18 (2017)](https://www.ea.com/games/madden-nfl/madden-nfl-18)\n  - [Need for Speed: Payback (2017)](https://www.ea.com/games/need-for-speed/need-for-speed-payback)\n- In the low latency trading infrastructure at [Charlesworth\n  Research](https://www.charlesworthresearch.com/) and [Marquette\n  Partners](https://www.marquettepartners.com/).\n\nIt's been cited by the following papers:\n- Peizhao Ou and Brian Demsky. 2018. Towards understanding the costs of avoiding\n  out-of-thin-air results. Proc. ACM Program. Lang. 2, OOPSLA, Article 136\n  (October 2018), 29 pages. DOI: https://doi.org/10.1145/3276506 \n\n## Example\n\n```cpp\nMPMCQueue\u003cint\u003e q(10);\nauto t1 = std::thread([\u0026] {\n  int v;\n  q.pop(v);\n  std::cout \u003c\u003c \"t1 \" \u003c\u003c v \u003c\u003c \"\\n\";\n});\nauto t2 = std::thread([\u0026] {\n  int v;\n  q.pop(v);\n  std::cout \u003c\u003c \"t2 \" \u003c\u003c v \u003c\u003c \"\\n\";\n});\nq.push(1);\nq.push(2);\nt1.join();\nt2.join();\n```\n\n## Usage\n\n- `MPMCQueue\u003cT\u003e(size_t capacity);`\n\n  Constructs a new `MPMCQueue` holding items of type `T` with capacity\n  `capacity`.\n  \n- `void emplace(Args \u0026\u0026... args);`\n\n  Enqueue an item using inplace construction. Blocks if queue is full.\n  \n- `bool try_emplace(Args \u0026\u0026... args);`\n\n  Try to enqueue an item using inplace construction. Returns `true` on\n  success and `false` if queue is full.\n\n- `void push(const T \u0026v);`\n\n  Enqueue an item using copy construction. Blocks if queue is full.\n\n- `template \u003ctypename P\u003e void push(P \u0026\u0026v);`\n\n  Enqueue an item using move construction. Participates in overload\n  resolution only if `std::is_nothrow_constructible\u003cT, P\u0026\u0026\u003e::value ==\n  true`. Blocks if queue is full.\n\n- `bool try_push(const T \u0026v);`\n\n  Try to enqueue an item using copy construction. Returns `true` on\n  success and `false` if queue is full.\n\n- `template \u003ctypename P\u003e bool try_push(P \u0026\u0026v);`\n\n  Try to enqueue an item using move construction. Participates in\n  overload resolution only if `std::is_nothrow_constructible\u003cT,\n  P\u0026\u0026\u003e::value == true`. Returns `true` on success and `false` if queue\n  is full.\n\n- `void pop(T \u0026v);`\n\n  Dequeue an item by copying or moving the item into `v`. Blocks if\n  queue is empty.\n  \n- `bool try_pop(T \u0026v);`\n\n  Try to dequeue an item by copying or moving the item into\n  `v`. Return `true` on sucess and `false` if the queue is empty.\n\n- `ssize_t size();`\n\n  Returns the number of elements in the queue.\n\n  The size can be negative when the queue is empty and there is at least one\n  reader waiting. Since this is a concurrent queue the size is only a best\n  effort guess until all reader and writer threads have been joined.\n\n- `bool empty();`\n\n  Returns true if the queue is empty.\n\n  Since this is a concurrent queue this is only a best effort guess until all\n  reader and writer threads have been joined.\n\nAll operations except construction and destruction are thread safe.\n\n## Implementation\n\n![Memory layout](https://github.com/rigtorp/MPMCQueue/blob/master/mpmc.png)\n\nEnqeue:\n\n1. Acquire next write *ticket* from *head*.\n2. Wait for our *turn* (2 * (ticket / capacity)) to write *slot* (ticket % capacity).\n3. Set *turn = turn + 1* to inform the readers we are done writing.\n\nDequeue:\n\n1. Acquire next read *ticket* from *tail*.\n2. Wait for our *turn* (2 * (ticket / capacity) + 1) to read *slot* (ticket % capacity).\n3. Set *turn = turn + 1* to inform the writers we are done reading.\n\n\nReferences:\n\n- *Daniel Orozco, Elkin Garcia, Rishi Khan, Kelly Livingston, and Guang R. Gao*. 2012. Toward high-throughput algorithms on many-core architectures. ACM Trans. Archit. Code Optim. 8, 4, Article 49 (January 2012), 21 pages. DOI: https://doi.org/10.1145/2086696.2086728\n- *Dave Dice*. 2014. [PTLQueue : a scalable bounded-capacity MPMC queue](https://blogs.oracle.com/dave/entry/ptlqueue_a_scalable_bounded_capacity).\n- *Oleksandr Otenko*. [US 8607249 B2: System and method for efficient concurrent queue implementation](http://www.google.com/patents/US8607249).\n- *Massimiliano Meneghin, Davide Pasetto, Hubertus Franke*. 2012. [Performance evaluation of inter-thread communication mechanisms on multicore/multithreaded architectures](http://researcher.watson.ibm.com/researcher/files/ie-pasetto_davide/PerfLocksQueues.pdf). DOI: https://doi.org/10.1145/2287076.2287098\n- *Paul E. McKenney*. 2010. [Memory Barriers: a Hardware View for Software Hackers](http://irl.cs.ucla.edu/~yingdi/web/paperreading/whymb.2010.06.07c.pdf).\n- *Dmitry Vyukov*. 2014. [Bounded MPMC queue](http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue).\n\n## Testing\n\nTesting concurrency algorithms is hard. I'm using two approaches to test the\nimplementation:\n\n- A single threaded test that the functionality works as intended,\n  including that the element constructor and destructor is invoked\n  correctly.\n- A multithreaded fuzz test that all elements are enqueued and\n  dequeued correctly under heavy contention.\n\n## TODO\n\n- [X] Add allocator supports so that the queue could be used with huge pages and\n  shared memory\n- [ ] Add benchmarks and compare to `boost::lockfree::queue` and others\n- [ ] Use C++20 concepts instead of `static_assert` if available\n- [X] Use `std::hardware_destructive_interference_size` if available\n- [ ] Add API for zero-copy deqeue and batch dequeue operations\n- [ ] Add `[[nodiscard]]` attributes\n\n## About\n\nThis project was created by [Erik Rigtorp](https://rigtorp.se)\n\u003c[erik@rigtorp.se](mailto:erik@rigtorp.se)\u003e.\n","funding_links":[],"categories":["C++","Libraries","Data Structures and Algorithms"],"sub_categories":["Threading"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frigtorp%2FMPMCQueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frigtorp%2FMPMCQueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frigtorp%2FMPMCQueue/lists"}