{"id":20105085,"url":"https://github.com/jdee/atomicvalue","last_synced_at":"2025-10-10T01:33:46.121Z","repository":{"id":138590218,"uuid":"192850548","full_name":"jdee/AtomicValue","owner":"jdee","description":"Fast synchronization primitives for C++","archived":false,"fork":false,"pushed_at":"2024-05-27T22:24:44.000Z","size":40,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-13T04:44:53.206Z","etag":null,"topics":["c-plus-plus","concurrency","pthreads","stl","synchronization","templates"],"latest_commit_sha":null,"homepage":null,"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/jdee.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-20T04:50:42.000Z","updated_at":"2024-11-21T09:43:42.000Z","dependencies_parsed_at":"2024-11-13T17:46:05.070Z","dependency_job_id":"5255b617-5472-4f2d-9a43-9b6c647db5b3","html_url":"https://github.com/jdee/AtomicValue","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/jdee%2FAtomicValue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdee%2FAtomicValue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdee%2FAtomicValue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdee%2FAtomicValue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdee","download_url":"https://codeload.github.com/jdee/AtomicValue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241549062,"owners_count":19980474,"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":["c-plus-plus","concurrency","pthreads","stl","synchronization","templates"],"created_at":"2024-11-13T17:45:46.858Z","updated_at":"2025-10-10T01:33:41.079Z","avatar_url":"https://github.com/jdee.png","language":"C++","readme":"```\n____________                 _____      ___    __      ______\n___    |_  /_____________ ______(_)_______ |  / /_____ ___  /___  ______\n__  /| |  __/  __ \\_  __ `__ \\_  /_  ___/_ | / /_  __ `/_  /_  / / /  _ \\\n_  ___ / /_ / /_/ /  / / / / /  / / /__ __ |/ / / /_/ /_  / / /_/ //  __/\n/_/  |_\\__/ \\____//_/ /_/ /_//_/  \\___/ _____/  \\__,_/ /_/  \\__,_/ \\___/\n```\n\n# AtomicValue\n\nFastAtomicReader and FastAtomicWriter templates for C++. Currently limited to\nPOSIX. Tested on iOS, macOS and Ubuntu. Limited features. Highly unsafe.\n\nFastAtomicReader and FastAtomicWriter are lightweight, asymmetric\nsynchronization primitives similar to std::atomic. In each case, either read\nor write operations are undelayable. Locking is accomplished using only an\ninteger counter. The FastAtomicReader template provides a shared read lock, but\nnot an exclusive write lock. FastAtomicWriter is the reverse. It provides a\nshared write lock with no read lock (and hence is best used when only one\nthread will ever perform write operations, and reads are few).\n\nWhile theoretically unsafe, these templates have been in use in [an iOS app on\nthe App Store](https://itunes.apple.com/us/app/cpyn/id929721548?mt=8) for five\nyears without event. They are prone to a peculiar kind\nof deadlock, but that was addressed before the app was launched. Since then,\nthey have performed admirably in a highly latency-sensitve app where they are\nused extensively.\n\nThey consistenly beat std::atomic in benchmarking by a significant factor, but\nat the cost of safety. Use with care!\n\n## Benchmark\n\n```bash\nmake check\n```\n\n```\n[jdee@Jimmy-Dees-MacBookPro AtomicValue (master)]$ make check\nc++ -std=c++11 -O3 -Wall -Werror -Iinclude   -c -o src/benchmark.o src/benchmark.cpp\nc++ -std=c++11 -O3 -Wall -Werror -Iinclude   -c -o src/log.o src/log.cpp\nc++ -std=c++11 -O3 -Wall -Werror -Iinclude   -c -o src/timeutil.o src/timeutil.cpp\nc++ -std=c++11 -O3 -Wall -Werror -Iinclude    src/benchmark.o src/log.o src/timeutil.o  -lpthread -o benchmark\n./benchmark 1000000\n2019-07-11-23:14:51.774524 ____________                 _____      ___    __      ______\n2019-07-11-23:14:51.775160 ___    |_  /_____________ ______(_)_______ |  / /_____ ___  /___  ______\n2019-07-11-23:14:51.775166 __  /| |  __/  __ \\_  __ `__ \\_  /_  ___/_ | / /_  __ `/_  /_  / / /  _ \\\n2019-07-11-23:14:51.775171 _  ___ / /_ / /_/ /  / / / / /  / / /__ __ |/ / / /_/ /_  / / /_/ //  __/\n2019-07-11-23:14:51.775175 /_/  |_\\__/ \\____//_/ /_/ /_//_/  \\___/ _____/  \\__,_/ /_/  \\__,_/ \\___/\n2019-07-11-23:14:51.775196\n2019-07-11-23:14:51.775200 Build type: Release\n2019-07-11-23:14:51.775209 loop count: 10000\n2019-07-11-23:14:51.775215 #####\n2019-07-11-23:14:51.775219 ##### unsynchronized\n2019-07-11-23:14:51.775222 #####\n2019-07-11-23:14:51.775227 starting test (100 x 10000)\n2019-07-11-23:14:51.777242 done ✅\n2019-07-11-23:14:51.777275 time elapsed: 2.004 ms\n2019-07-11-23:14:51.777285 rate: 499.002 ± 22.4951 MHz (σ = 224.951 MHz)\n2019-07-11-23:14:51.777291 per loop: 2.004 ± 0.0903408 ns (σ = 0.903408 ns)\n2019-07-11-23:14:51.777297 #####\n2019-07-11-23:14:51.777301 ##### FastAtomicReader\n2019-07-11-23:14:51.777304 #####\n2019-07-11-23:14:51.777308 starting test (100 x 10000)\n2019-07-11-23:14:51.780451 done ✅\n2019-07-11-23:14:51.780486 time elapsed: 1.665 ms\n2019-07-11-23:14:51.780494 rate: 600.601 ± 19.6667 MHz (σ = 196.667 MHz)\n2019-07-11-23:14:51.780521 per loop: 1.665 ± 0.0545204 ns (σ = 0.545204 ns)\n2019-07-11-23:14:51.780527 #####\n2019-07-11-23:14:51.780530 ##### FastAtomicWriter\n2019-07-11-23:14:51.780534 #####\n2019-07-11-23:14:51.780538 starting test (100 x 10000)\n2019-07-11-23:14:51.784263 done ✅\n2019-07-11-23:14:51.784280 time elapsed: 2.247 ms\n2019-07-11-23:14:51.784288 rate: 445.038 ± 9.80625 MHz (σ = 98.0625 MHz)\n2019-07-11-23:14:51.784296 per loop: 2.247 ± 0.0495119 ns (σ = 0.495119 ns)\n2019-07-11-23:14:51.784307 #####\n2019-07-11-23:14:51.784311 ##### std::atomic\n2019-07-11-23:14:51.784315 #####\n2019-07-11-23:14:51.784320 starting test (100 x 10000)\n2019-07-11-23:14:51.790396 done ✅\n2019-07-11-23:14:51.790428 time elapsed: 6.066 ms\n2019-07-11-23:14:51.790435 rate: 164.853 ± 3.86797 MHz (σ = 38.6797 MHz)\n2019-07-11-23:14:51.790441 per loop: 6.066 ± 0.142327 ns (σ = 1.42327 ns)\n2019-07-11-23:14:51.790447 #####\n2019-07-11-23:14:51.790451 ##### mutex\n2019-07-11-23:14:51.790454 #####\n2019-07-11-23:14:51.790458 starting test (100 x 10000)\n2019-07-11-23:14:51.829111 done ✅\n2019-07-11-23:14:51.829147 time elapsed: 38.563 ms\n2019-07-11-23:14:51.829158 rate: 25.9316 ± 0.253256 MHz (σ = 2.53256 MHz)\n2019-07-11-23:14:51.829166 per loop: 38.563 ± 0.376619 ns (σ = 3.76619 ns)\n```\n\n## Usage\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cAtomicValue/AtomicValue.h\u003e\n\nusing namespace std;\n\nAtomicValue::FastAtomicReader\u003cunsigned int\u003e counter(0);\n\n++ counter;\n\ncout \u003c\u003c \"counter = \" \u003c\u003c counter \u003c\u003c endl;\n\n```\n\n## Use of volatile\n\nA FastAtomicReader or FastAtomicWriter may be declared `volatile`. However, note\nthat certain operators and member functions that would ordinarily return\nreferences to `*this` would have to return references to volatiles. This\ngenerates warnings from G++ in calling code in situations like this:\n\n```cpp\nFastAtomicReader\u003cint\u003e volatile x;\nx = 1;\n```\n\nThe return type of the assignment would be `FastAtomicReader\u003cint\u003e volatile\u0026`.\nThe return value is unread, which generates a warning at the second line. The\nwarning can't be silenced in the AtomicValue source code. As a result, the\nvolatile versions of these functions and operators do not return a value, e.g.\n\n```cpp\nFastAtomicReader\u0026 set(T data);\nvoid set(T data) volatile;\n```\n\nSo:\n\n```cpp\nFastAtomicReader\u003cint\u003e x;\n// compiles\ncout \u003c\u003c \"x = \" \u003c\u003c x.set(1) \u003c\u003c endl;\n```\n\nbut:\n\n```cpp\nFastAtomicReader\u003cint\u003e volatile x;\n// does not compile\ncout \u003c\u003c \"x = \" \u003c\u003c x.set(1) \u003c\u003c endl;\n```\n\nInstead:\n\n```cpp\nFastAtomicReader\u003cint\u003e volatile x;\n// compiles\nx.set(1);\ncout \u003c\u003c \"x = \" \u003c\u003c x \u003c\u003c endl;\n```\n\nOperators/functions that return void on volatile objects:\n\n- `set(T)`\n- `operator+=(int)`\n- `operator++()`\n- `operator=(T)`\n- `operator=(const FastAtomicReader/Writer\u0026)`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdee%2Fatomicvalue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdee%2Fatomicvalue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdee%2Fatomicvalue/lists"}