{"id":22796426,"url":"https://github.com/macmade/cppatomic","last_synced_at":"2025-04-19T13:13:06.070Z","repository":{"id":140400176,"uuid":"47710734","full_name":"macmade/CPPAtomic","owner":"macmade","description":"Replacement of std::atomic supporting non trivially-copyable types","archived":false,"fork":false,"pushed_at":"2024-05-07T15:40:16.000Z","size":256,"stargazers_count":16,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T08:11:19.533Z","etag":null,"topics":["atomic","atomicity","c-plus-plus","generic","lock"],"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/macmade.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":"macmade"}},"created_at":"2015-12-09T18:30:43.000Z","updated_at":"2025-03-18T22:35:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"e1bd06a3-0e65-472c-909b-b87e1a0e6d79","html_url":"https://github.com/macmade/CPPAtomic","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/macmade%2FCPPAtomic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macmade%2FCPPAtomic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macmade%2FCPPAtomic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macmade%2FCPPAtomic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/macmade","download_url":"https://codeload.github.com/macmade/CPPAtomic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249701561,"owners_count":21312757,"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":["atomic","atomicity","c-plus-plus","generic","lock"],"created_at":"2024-12-12T05:12:57.810Z","updated_at":"2025-04-19T13:13:06.039Z","avatar_url":"https://github.com/macmade.png","language":"C++","funding_links":["https://github.com/sponsors/macmade"],"categories":[],"sub_categories":[],"readme":"CPPAtomic\n=========\n\n[![Build Status](https://img.shields.io/github/actions/workflow/status/macmade/CPPAtomic/ci-mac.yaml?label=macOS\u0026logo=apple)](https://github.com/macmade/CPPAtomic/actions/workflows/ci-mac.yaml)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/macmade/CPPAtomic/ci-win.yaml?label=Windows\u0026logo=windows)](https://github.com/macmade/CPPAtomic/actions/workflows/ci-win.yaml)\n[![Issues](http://img.shields.io/github/issues/macmade/CPPAtomic.svg?logo=github)](https://github.com/macmade/CPPAtomic/issues)\n![Status](https://img.shields.io/badge/status-active-brightgreen.svg?logo=git)\n![License](https://img.shields.io/badge/license-mit-brightgreen.svg?logo=open-source-initiative)  \n[![Contact](https://img.shields.io/badge/follow-@macmade-blue.svg?logo=twitter\u0026style=social)](https://twitter.com/macmade)\n[![Sponsor](https://img.shields.io/badge/sponsor-macmade-pink.svg?logo=github-sponsors\u0026style=social)](https://github.com/sponsors/macmade)\n\nAbout\n-----\n\nReplacement of `std::atomic` supporting **non trivially-copyable** types.\n\n### Rationale\n\nC++11 introduced the awesome [`std::atomic`](http://en.cppreference.com/w/cpp/atomic/atomic) template.\nUnfortunately, it can only be used with [**non trivially-copyable**](http://en.cppreference.com/w/cpp/concept/TriviallyCopyable) types.\n\nThis restricts its usage to primitive types and [POD](http://en.cppreference.com/w/cpp/concept/PODType) types, meaning you can't use `std::atomic` with most C++ classes (user-defined or STL).\n\n**CPPAtomic addresses this issue by providing a new template that can be used with non trivially-copyable types as well ass trivially-copyable types.**\n\nAs an example, assuming the following declarations:\n    \n    struct s\n    {\n        int x;\n    };\n    \n    class A\n    {};\n    \n    class B\n    {\n        public:\n            \n            B( void );\n            B( const B \u0026 rhs );\n    };\n    \nHere's what will happen with `std::atomic`:\n    \n    std::atomic\u003c struct s \u003e    s;   /* OK - POD type */\n    std::atomic\u003c A \u003e           a;   /* OK - POD type */\n    std::atomic\u003c B \u003e           b;   /* Error - Class B is not trivially-copyable */\n    std::atomic\u003c std::string \u003e str; /* Error - std::string is not trivially-copyable */\n\nIn that example, class `B` is not trivially-copyable due to its copy constructor, so it cannot be used with `std::atomic`.\n\nUsing `XS::Atomic` instead, everything will compile and be fine:\n\n    XS::Atomic\u003c struct s \u003e    s;   /* OK */\n    XS::Atomic\u003c A \u003e           a;   /* OK */\n    XS::Atomic\u003c B \u003e           b;   /* OK */\n    XA::Atomic\u003c std::string \u003e str; /* OK */\n\n### Operators\n\n`XS::Atomic` overloads all operators, but uses type traits to enable specific overloads, depending on the type.\n\nFor instance, the following is valid, and atomic:\n\n    XS::Atomic\u003c int \u003e          i{ 42 };\n    XS::Atomic\u003c unsigned int \u003e u{ 42 };\n    \n    i++;\n    u \u0026= 0xFF;\n\nThe following is not (compilation error):\n\n    XS::Atomic\u003c double \u003e d{ 42 };\n    \n    d \u0026= 0xFF;\n\nBitwise operations make no sense with floating point value, so such overloads are disabled when using a floating point type.\n\nWhen using classes, usual operators are also detected using type traits, and available if they are implemented:\n\n    class Foo\n    {};\n    \n    class Bar\n    {\n        public:\n            \n            Bar \u0026 operator +=( const Bar \u0026 rhs );\n    };\n    \n    XS::Atomic\u003c Foo \u003e f;\n    XS::Atomic\u003c Bar \u003e b;\n    \n    f += Foo(); /* Compiler error - Foo has no such operator */\n    b += Bar(); /* Bar::operator+= will be used, atomically */\n\n### Initialisation\n\nAll `XS::Atomic` objects initialise their values to a default one, using C++11 value initialisation (`{}`)..\n\nAs an example, an `XS::Atomic\u003c int \u003e` will default to `0`.\nStructure types are zero-initialised as well.\nFor classes, the default constructor will be used.\n\n### Implementation details\n\n`XS::Atomic` internally ensures locking for all types.\nIt will use a `std::recursive_mutex`, along with `std::lock_guard` and `std::lock`.\n\nThis is necessary in order to allow operations on non trivially-copyable types, as well as operations not implemented by `std::atomic` on primitive types (see section about operators).\n\nYou might still prefer `std::atomic` for primitive types, if you're concerned about potential performance issues and if your implementation provides lock-free atomicity.\n\nLicense\n-------\n\nCPPAtomic is released under the terms of the MIT license.\n\nRepository Infos\n----------------\n\n    Owner:\t\t\tJean-David Gadina - XS-Labs\n    Web:\t\t\twww.xs-labs.com\n    Blog:\t\t\twww.noxeos.com\n    Twitter:\t\t@macmade\n    GitHub:\t\t\tgithub.com/macmade\n    LinkedIn:\t\tch.linkedin.com/in/macmade/\n    StackOverflow:\tstackoverflow.com/users/182676/macmade\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacmade%2Fcppatomic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmacmade%2Fcppatomic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacmade%2Fcppatomic/lists"}