{"id":22000529,"url":"https://github.com/rubnsbarbosa/sample-threads","last_synced_at":"2025-07-29T22:13:39.931Z","repository":{"id":244585710,"uuid":"815508388","full_name":"rubnsbarbosa/sample-threads","owner":"rubnsbarbosa","description":"rust sample threads 🦀","archived":false,"fork":false,"pushed_at":"2024-06-16T02:02:01.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-20T17:59:26.690Z","etag":null,"topics":["concurrency","multithreading","mutex-lock","rust","thread-safety"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/rubnsbarbosa.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":"2024-06-15T11:18:50.000Z","updated_at":"2025-03-09T16:49:22.000Z","dependencies_parsed_at":"2024-06-15T20:49:09.377Z","dependency_job_id":"64fc4539-c2ca-43ff-b036-4da8f3b12995","html_url":"https://github.com/rubnsbarbosa/sample-threads","commit_stats":null,"previous_names":["rubnsbarbosa/sample-threads"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubnsbarbosa%2Fsample-threads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubnsbarbosa%2Fsample-threads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubnsbarbosa%2Fsample-threads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubnsbarbosa%2Fsample-threads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rubnsbarbosa","download_url":"https://codeload.github.com/rubnsbarbosa/sample-threads/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245060365,"owners_count":20554486,"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","multithreading","mutex-lock","rust","thread-safety"],"created_at":"2024-11-29T23:09:36.224Z","updated_at":"2025-03-23T05:25:10.210Z","avatar_url":"https://github.com/rubnsbarbosa.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Concurrency and Parallelism with Rust\n\nThis repository contains a collection of Rust projects demonstrating some concepts in concurrency and parallelism. Each project is organized as a separate \npackage within a Cargo workspace.\n\n### Getting Started\n\nTo get started with these projects, ensure you have [Rust](https://www.rust-lang.org/) installed on your machine. You can install Rust using `rustup` run the \nfollowing command in your terminal, then follow the onscreen instructions.\n\n```shell\ncurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\n```\n\n### Executing the code\n\nunder the root workspace execute the cargo command below\n\n```shell\ncargo run --bin \u003cproject-name\u003e # e.g., _00-new-thread-with-spawn\n```\n\nor, inside the project directory `cargo run`\n\n### Projects overview\n\nHere you can find an overview about the current codes.  \n\n#### _00-new-thread-with-spawn\n\nIn this project we create multiple threads using the **thread::spawn** function from the standard library. Each thread print \"hey, {thread_identifier} either \nfrom main or spawn thread\". We also learn how to keep threads alive until all of they are all executed.\n\n```rust\nlet handle = thread::spawn(move || {\n    println!(\"Hey, from thread {}\", i);\n});\n```\n\n#### _01-sum-array-in-parallel\n\nThis project demonstrates how to sum all the elements of an array in parallel using multiple threads. It also use data decomposition by dividing the vector into \nchunks.\n\n```rust\nfor chunck in vector.chunks(chunk_vec_size) {\n    let chunk = chunck.to_owned();\n    vec_threads.push(thread::spawn(move || -\u003e i32 {\n        chunk.iter().sum()\n    }));\n}\n```\n\n#### _02-thread-safety-arc-mut\n\nThis project implements a thread-safe counter using a `Mutex` to ensure safe increments among multiple threads and `Arc` used to share ownership of the \n`Mutex`. Mutex ensures that multiple threads can safely increment the counter without causing race conditions.   \n\nSee [Mutex](https://doc.rust-lang.org/std/sync/struct.Mutex.html)  \n\n* Mutex allows only one thread to access the counter at a time;  \n* Each thread gets a clone of the Arc pointer, incrementing the reference count;  \n* Before accessing the counter, each thread locks the Mutex using `lock()`. This ensures that only one thread can modify the counter at a time;  \n* After all threads have finished, the counter result is printed.   \n\n#### _03-producer-consumer-buffer\n\nThe producer-consumer problem is a classic example of a multi-threading problem where a fixed-size buffer is shared between producer threads that generate data \nand consumer threads that process data.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](https://github.com/rubnsbarbosa/sample-threads/tree/main?tab=MIT-1-ov-file) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubnsbarbosa%2Fsample-threads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frubnsbarbosa%2Fsample-threads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubnsbarbosa%2Fsample-threads/lists"}