{"id":20384339,"url":"https://github.com/gyakobo/multi-threading","last_synced_at":"2026-03-01T14:02:57.987Z","repository":{"id":242611523,"uuid":"810043586","full_name":"Gyakobo/multi-threading","owner":"Gyakobo","description":"This project was made to showcase a sample example of muli-threading in the C programming language.","archived":false,"fork":false,"pushed_at":"2025-02-27T17:10:11.000Z","size":148,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-23T23:39:02.525Z","etag":null,"topics":["c","function-approximation","integrals","integration","multithreading","number-pi","parallel-computing"],"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/Gyakobo.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-06-04T00:03:38.000Z","updated_at":"2025-02-27T17:10:15.000Z","dependencies_parsed_at":"2025-09-28T11:00:49.376Z","dependency_job_id":null,"html_url":"https://github.com/Gyakobo/multi-threading","commit_stats":null,"previous_names":["gyakobo/multi-threading"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Gyakobo/multi-threading","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2Fmulti-threading","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2Fmulti-threading/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2Fmulti-threading/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2Fmulti-threading/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gyakobo","download_url":"https://codeload.github.com/Gyakobo/multi-threading/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gyakobo%2Fmulti-threading/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29970543,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T13:32:00.443Z","status":"ssl_error","status_checked_at":"2026-03-01T13:32:00.084Z","response_time":124,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["c","function-approximation","integrals","integration","multithreading","number-pi","parallel-computing"],"created_at":"2024-11-15T02:27:23.484Z","updated_at":"2026-03-01T14:02:57.946Z","avatar_url":"https://github.com/Gyakobo.png","language":"C","readme":"# Multi-Threading or Parallelism\n\n![image](https://img.shields.io/badge/C-00599C?style=for-the-badge\u0026logo=c\u0026logoColor=white)\n![image](https://img.shields.io/badge/C%2B%2B-00599C?style=for-the-badge\u0026logo=c%2B%2B\u0026logoColor=white)\n![image](https://img.shields.io/badge/CMake-064F8C?style=for-the-badge\u0026logo=cmake\u0026logoColor=white)\n![image](https://img.shields.io/badge/windows%20terminal-4D4D4D?style=for-the-badge\u0026logo=windows%20terminal\u0026logoColor=white)\n\nauthor: [Andrew Gyakobo](https://github.com/Gyakobo)\n\nThis project was made to showcase a sample example of muli-threading in the C programming language. To be more exact, in this project we'll be trying to approximate the value $\\pi$. \n\n## Introduction\n\nMulti-threading is a programming concept where multiple threads are spawned by a process to execute tasks concurrently. Each thread runs independently but shares the process's resources like memory and file handles. Multi-threading can lead to more efficient use of resources, faster execution of tasks, and improved performance in multi-core systems.\n\n### Key Concepts\n1. Thread: A lightweight process or the smallest unit of execution within a process.\n1. Concurrency vs. Parallelism: Concurrency means multiple threads make progress at the same time, while parallelism means multiple threads run simultaneously on different cores.\n1. Synchronization: Mechanism to control the access of multiple threads to shared resources.\nThread Safety: Ensuring that shared data is accessed by only one thread at a time.\n\n## Methodology\n\nWe'll be utilizing the function $\\dfrac{4}{1 + x^2}$, the integral of which is a near approximation of $\\pi$. Thus we'll be calculating the following formula:\n\n$$\n\\int \\dfrac{4}{1 + x^2} dx \\approx \\pi\n$$\n\n\u003e[!NOTE]\n\u003eThe graph below showcases the integrated function. \n\u003cimg src=\"./assets/function.png\"\u003e\n\nThere is of course a minor issue with this calculation. In particular, as the $dx$ component gets ever smaller, the integration gets more precise. Hence it becomes a priority to make the $dx$ as small as possible. This however certainly backfires as with the decreasing $dx$ the integration becomes more complex as there are more facets in the function to compute. \n\nHere is a more specific example of the aforementioned computation, this isn't a representation of the previously calculated function but still comminucates the same idea: \n\n\u003cimg src=\"./assets/function_example.jpeg\"\u003e\n\nAs you can witness, the integration is just a summation of all the rectangles entangled under the function. This is roughly what is being calculated:\n\n$f(x_{i})$ - the function $4/(1 + x^2)$\n\n$\\Delta x$ - is the select width of the individual squares that we have to compute.\n\n$$\n\\displaystyle\\sum\\limits_{i=0}^{\\infty} f(x_{i}) \\Delta x \\approx \\pi\n$$\n\nFrom here we can distinctly see that the smaller the $dx$, the more rectangular areas we have have to compute and add up. This however proves to be a challenge cause the more the rectangles the more the computation, and we know that it is essential to have an enormous amount of said shapes.\n\nHenceforth, a viable solution to generate as much rectangles as possible would be to use parallelism and multi-core processing with the C library `\u003comp.h\u003e`.\n\n## Code snippets\n\n* From the getgo the code greets us with two include statements:\n\n```c \n#include \u003comp.h\u003e\n#include \u003cstdio.h\u003e\n```\n\n* Furthermore, we define the `const int num_steps` *(the quantity of rectangles, the area of which shall be integrated)* and then the `double step` *(the dx or the width of each rectangle)*\n\n* Now entering the main scope of our program we initialize the multi-threading aspect using the `#pragma omp parallel` where each so-called thread runs simeaultaneously from one another and calculates the partial area `local_area`.\n\n```c\n#pragma omp parallel\n{\n    int id =    omp_get_thread_num();\n    int n =     omp_get_num_threads();\n    int i;\n    double local_area = 0;\n\n    for (i = id; i\u003cnum_steps; i+=n) {\n        double x = (i + 0.5) * step;\n        double y = 4 / (1 + x*x);\n        local_area += step * y;\n    }\n    ...\n```\n\n* After calculating the `local_area` all the threads are simeaultaneously halted in one specific scope where they all perform one command; in our case sum up all the partial areas into one `double area` given us our final result.\n\n```c\n#pragma omp critical\n{\n    area += local_area;\n}\n```\n\n\u003e[!IMPORTANT]\n\u003eIt is important to acquiesce that before running this program you need to fathom and fully understand the limits of your PC set before making such calculations.\n\n## The OpenMP - open Multi-processing library \n\nJust as a side note the **OpenMP** library comprises of the following parts. Also feel free to download, edit, commit and leave feedback to the project.\n\n### Compiler Directives\n\n```c\n#pragma omp parallel\n#pragma omp critical\n#pragma omp barrier\n#pragma omp master\n```\n\n### Functions\n\n```c\n#include \u003comp.h\u003e\nint omp_get_thread_num()\nint omp_get_num_threads()\n```\n\n### Compiling and Linking\n\n```bash\ngcc -fopenmp # C compiler\ng++ -fopenmp # C++ compiler\n```\n\n### Environmental variables\n\n```bash\nexport OMP_NUM_THREADS=8\nexport OMP_NESTED=TRUE\n```\n\n## License\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyakobo%2Fmulti-threading","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgyakobo%2Fmulti-threading","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyakobo%2Fmulti-threading/lists"}