{"id":20093751,"url":"https://github.com/lbrndnr/swift-stm","last_synced_at":"2025-10-25T04:02:54.293Z","repository":{"id":63916382,"uuid":"96038726","full_name":"lbrndnr/swift-stm","owner":"lbrndnr","description":"An optimistic STM written in Swift","archived":false,"fork":false,"pushed_at":"2019-01-06T20:54:34.000Z","size":82,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-05T07:47:20.932Z","etag":null,"topics":["concurrency","stm","swift","thread-safety"],"latest_commit_sha":null,"homepage":null,"language":"Swift","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/lbrndnr.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":"2017-07-02T17:28:12.000Z","updated_at":"2024-07-03T08:42:45.000Z","dependencies_parsed_at":"2023-01-14T13:45:53.034Z","dependency_job_id":null,"html_url":"https://github.com/lbrndnr/swift-stm","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/lbrndnr/swift-stm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbrndnr%2Fswift-stm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbrndnr%2Fswift-stm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbrndnr%2Fswift-stm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbrndnr%2Fswift-stm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lbrndnr","download_url":"https://codeload.github.com/lbrndnr/swift-stm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbrndnr%2Fswift-stm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280901420,"owners_count":26410586,"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","status":"online","status_checked_at":"2025-10-25T02:00:06.499Z","response_time":81,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","stm","swift","thread-safety"],"created_at":"2024-11-13T16:48:19.072Z","updated_at":"2025-10-25T04:02:54.277Z","avatar_url":"https://github.com/lbrndnr.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# swift-stm\n\n[![Twitter: @lbrndnr](https://img.shields.io/badge/contact-@lbrndnr-blue.svg?style=flat)](https://twitter.com/lbrndnr)\n[![License](http://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://github.com/lbrndnr/ImagePickerSheetController/blob/master/LICENSE)\n\n## About\nswift-stm is an optimistic and lock free [software transactional memory](https://en.wikipedia.org/wiki/Software_transactional_memory) written in Swift. It's very rudimental and really only a draft as of now. I wouldn't be using it in production code if I were you. Feel free to try it out though :)\n\n## What does it do exactly?\n\nAn STM allows you to write thread safe code by making blocks of code seem to be executed atomically. This means that the transaction is either executed in a given point in time or not at all. In reality, however, it just executes the transaction and notices potential collisions. \nThe idea behind this is that in real life, collisions are rare and locks a bit of an overhead. This means that (technically) STMs are a lot easier to use (no deadlocks, better performance yada yada) than traditional locks.\nNote that this is just a draft, I can't guarantee mutex nor performance!\n\n## So how do I use it?\n\nSo instead of using Dispatch...\n\n```swift\nfunc transfer(from: Account, to: Account, amount: Int) -\u003e Bool {\n    var res = false\n    \n    queue.async {            \n        let i = from.balance\n        \n        guard i \u003e= amount else {\n            return\n        }\n        \n        from.balance = i - amount\n       \tto.balance = to.balance + amount\n        \n        res = true\n    }\n    \n   return res\n}\n```\n\n... you'd write something like this\n\n```swift\nfunc transfer(from: Account, to: Account, amount: Int) -\u003e Bool {\n    var res = false\n    \n    atomic {            \n        let i = try from.balance.get()\n        \n        guard i \u003e= amount else {\n            return\n        }\n        \n        try from.balance.set(i - amount)\n        try to.balance.set(to.balance.get() + amount)\n        \n        res = true\n    }\n    \n    return res\n}\n```\n\nI must admit that the syntax is a bit clumsy as of now...\n\n## License\nswift-stm is licensed under the [MIT License](http://opensource.org/licenses/mit-license.php).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flbrndnr%2Fswift-stm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flbrndnr%2Fswift-stm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flbrndnr%2Fswift-stm/lists"}