{"id":28034861,"url":"https://github.com/typeerrorengine2022/threadsafecounter","last_synced_at":"2025-05-11T11:58:24.843Z","repository":{"id":291984815,"uuid":"979427848","full_name":"TypeErrorEngine2022/ThreadSafeCounter","owner":"TypeErrorEngine2022","description":"Thread-Safe Counter in C++","archived":false,"fork":false,"pushed_at":"2025-05-07T17:10:38.000Z","size":96,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-11T11:58:19.762Z","etag":null,"topics":["concurrency","cpp","multithreading"],"latest_commit_sha":null,"homepage":"","language":"C++","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/TypeErrorEngine2022.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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,"zenodo":null}},"created_at":"2025-05-07T13:49:38.000Z","updated_at":"2025-05-07T17:10:42.000Z","dependencies_parsed_at":"2025-05-07T15:23:43.804Z","dependency_job_id":"00afb3cd-b237-4662-afe9-ebd8ad92046e","html_url":"https://github.com/TypeErrorEngine2022/ThreadSafeCounter","commit_stats":null,"previous_names":["typeerrorengine2022/threadsafecounter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypeErrorEngine2022%2FThreadSafeCounter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypeErrorEngine2022%2FThreadSafeCounter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypeErrorEngine2022%2FThreadSafeCounter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypeErrorEngine2022%2FThreadSafeCounter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TypeErrorEngine2022","download_url":"https://codeload.github.com/TypeErrorEngine2022/ThreadSafeCounter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253561113,"owners_count":21927764,"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","multithreading"],"created_at":"2025-05-11T11:58:24.289Z","updated_at":"2025-05-11T11:58:24.831Z","avatar_url":"https://github.com/TypeErrorEngine2022.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ThreadSafeCounter\n\nDetailed blog for this repository is on [HackMD](https://hackmd.io/@JackyDev/r1BhCytleg!)!\n\nThis repository implmented 3 versions of thread-safe counter\n- NonThreadSafeCounter\n- ThreadSafeCounter\n- ThreadSafeCounterWithSubCounter\n\n## Unique Public Methods of `ThreadSafeCounterWithSubCounter`\n- `register_counter()`: Registers a new sub-counter.\n\n## Common Public Methods\nAll implementations have following public methods:\n- `increment()`: Increments the counter by 1.\n  - for `ThreadSafeCounterWithSubCounter`, the thread must register a sub-counter before calling this method.\n- `get_count()`: Returns the total current count.\n\n## NonThreadSafeCounter\nSince this is not thread-safe, it will have data race upon usage, which yields incorrect count. So, no benchmark is provided for this version.\n\n## Benchmark for the thread-safe versions\nRun the benchmark using the following command:\n```bash\ng++ --std=c++20 --pedantic ./src/counter-benchmark.cpp -o benchmark.out\n./benchmark.out\n```\n\n## Benchmark Output for the thread-safe versions\nWhen the number of thread is higher, the speedup of `ThreadSafeCounterWithSubCounter` is more significant than `ThreadSafeCounter`.\n\n![speedup](./assets/speedup-ratio.png)\n![threadcount-vs-time](./assets/threadcount-vs-time.png)\n\n```\n============================================\nCounter Performance Benchmark\nOperations per thread: 100000\nIterations per test: 5\n============================================\n\nTesting with 1 threads:\n--------------------------------------------\nThreadSafeCounter:\n  Min time: 1924 µs\n  Max time: 2597 µs\n  Avg time: 2415 µs\n  Median:   2523 µs\n\nThreadSafeCounterWithSubCounter:\n  Min time: 2754 µs\n  Max time: 3550 µs\n  Avg time: 3113 µs\n  Median:   3160 µs\n\nPerformance comparison:\n  Speedup ratio (ThreadSafeCounter/ThreadSafeCounterWithSubCounter): 0.80x\n  The ThreadSafeCounter implementation is 25.25% faster\n\n============================================\n\nTesting with 2 threads:\n--------------------------------------------\nThreadSafeCounter:\n  Min time: 9031 µs\n  Max time: 12694 µs\n  Avg time: 10481 µs\n  Median:   9876 µs\n\nThreadSafeCounterWithSubCounter:\n  Min time: 2644 µs\n  Max time: 2777 µs\n  Avg time: 2699 µs\n  Median:   2690 µs\n\nPerformance comparison:\n  Speedup ratio (ThreadSafeCounter/ThreadSafeCounterWithSubCounter): 3.67x\n  The ThreadSafeCounterWithSubCounter implementation is 267.14% faster\n\n============================================\n\nTesting with 4 threads:\n--------------------------------------------\nThreadSafeCounter:\n  Min time: 17724 µs\n  Max time: 19491 µs\n  Avg time: 18384 µs\n  Median:   18360 µs\n\nThreadSafeCounterWithSubCounter:\n  Min time: 2753 µs\n  Max time: 2779 µs\n  Avg time: 2765 µs\n  Median:   2763 µs\n\nPerformance comparison:\n  Speedup ratio (ThreadSafeCounter/ThreadSafeCounterWithSubCounter): 6.64x\n  The ThreadSafeCounterWithSubCounter implementation is 564.50% faster\n\n============================================\n\nTesting with 8 threads:\n--------------------------------------------\nThreadSafeCounter:\n  Min time: 39950 µs\n  Max time: 43707 µs\n  Avg time: 41552 µs\n  Median:   41416 µs\n\nThreadSafeCounterWithSubCounter:\n  Min time: 2800 µs\n  Max time: 3257 µs\n  Avg time: 2954 µs\n  Median:   2897 µs\n\nPerformance comparison:\n  Speedup ratio (ThreadSafeCounter/ThreadSafeCounterWithSubCounter): 14.30x\n  The ThreadSafeCounterWithSubCounter implementation is 1329.62% faster\n\n============================================\n\nTesting with 16 threads:\n--------------------------------------------\nThreadSafeCounter:\n  Min time: 64594 µs\n  Max time: 101465 µs\n  Avg time: 76240 µs\n  Median:   65821 µs\n\nThreadSafeCounterWithSubCounter:\n  Min time: 2840 µs\n  Max time: 3513 µs\n  Avg time: 3047 µs\n  Median:   2953 µs\n\nPerformance comparison:\n  Speedup ratio (ThreadSafeCounter/ThreadSafeCounterWithSubCounter): 22.29x\n  The ThreadSafeCounterWithSubCounter implementation is 2128.95% faster\n\n============================================\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypeerrorengine2022%2Fthreadsafecounter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypeerrorengine2022%2Fthreadsafecounter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypeerrorengine2022%2Fthreadsafecounter/lists"}