{"id":32408056,"url":"https://github.com/zelang-dev/c-threads","last_synced_at":"2026-07-06T18:31:24.670Z","repository":{"id":319729814,"uuid":"1079441857","full_name":"zelang-dev/c-threads","owner":"zelang-dev","description":"Emulated C11 threads \u0026 atomics, and features for cross-platform development","archived":false,"fork":false,"pushed_at":"2025-10-19T22:58:54.000Z","size":784,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-20T02:59:12.743Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/zelang-dev.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":"2025-10-19T20:10:54.000Z","updated_at":"2025-10-19T22:58:57.000Z","dependencies_parsed_at":"2025-10-27T23:16:31.985Z","dependency_job_id":null,"html_url":"https://github.com/zelang-dev/c-threads","commit_stats":null,"previous_names":["zelang-dev/c-threads"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/zelang-dev/c-threads","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zelang-dev%2Fc-threads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zelang-dev%2Fc-threads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zelang-dev%2Fc-threads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zelang-dev%2Fc-threads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zelang-dev","download_url":"https://codeload.github.com/zelang-dev/c-threads/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zelang-dev%2Fc-threads/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35202785,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-06T02:00:07.184Z","response_time":106,"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":[],"created_at":"2025-10-25T13:30:38.059Z","updated_at":"2026-07-06T18:31:24.444Z","avatar_url":"https://github.com/zelang-dev.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# C Threads\n\nA `C` *library combining multiple packages for cross-platform development:*\n\n- custom malloc with [rpmalloc](https://github.com/zelang-dev/rpmalloc).\n- emulated C11 [thread](https://en.cppreference.com/w/c/thread) using [Pthreads](https://en.wikipedia.org/wiki/Pthreads), or [Pthreads4w](http://sourceforge.net/projects/pthreads4w/).\n- emulated C11 [Atomic](https://en.cppreference.com/w/c/atomic) with [c-atomic](https://github.com/zelang-dev/c-atomic).\n- general `Linux` compatibility *headers* for **Windows**, including a [mmap](https://man7.org/linux/man-pages/man2/mmap.2.html) compilation.\n\nThe **deps** folder has *Windows* **build/fork** of [pthread-win32](https://github.com/GerHobbelt/pthread-win32).\n\nThis library is a minimal, portable implementation of basic *threading* and *atomics* for `C`. They closely mimic the functionality and naming of the standard **C11 Thread**, and **C11 Atomic**, should be easily replaceable with the corresponding standard variants.\n\nCustom **malloc** is a fork [rpmalloc](https://github.com/zelang-dev/rpmalloc) has no dependency on `thread_local` and can be compiled using [Tiny C compiler](https://github.com/zelang-dev/tinycc). All `malloc` operations are [lockless](https://preshing.com/20120612/an-introduction-to-lock-free-programming/), and implements a emulated **Thread-local storage** via macro `tls_storage(type, variable)` as:\n\n_example.h_\n\n```h\n#include \u003ccthreads.h\u003e\n#include \u003crpmalloc.h\u003e\n#include \u003cstdio.h\u003e\n#include \u003cassert.h\u003e\ntls_storage_extern(int, gLocalVar);\n```\n\n_example.c_\n\n```c\n#include \"example.h\"\n#define THREAD_COUNT 5\n\ntls_storage(int, gLocalVar)\n\nstatic int thread_local_storage(void *aArg) {\n    int thread = *(int *)aArg;\n    free(aArg);\n\n    int data = thread + rand();\n    *gLocalVar() = data;\n    usleep(10);\n    printf(\"thread #%d, gLocalVar is: %d\\n\", thread, *gLocalVar());\n    assert(*gLocalVar() == data);\n    return 0;\n}\n\nvoid emulated_tls(void) {\n    thrd_t t[THREAD_COUNT];\n    *gLocalVar() = 1;\n\n    for (int i = 0; i \u003c THREAD_COUNT; i++) {\n        int *n = malloc(sizeof * n);\n        *n = i;\n        thrd_create(t + i, thread_local_storage, n);\n    }\n\n    for (int i = 0; i \u003c THREAD_COUNT; i++) {\n        thrd_join(t[i], NULL);\n    }\n\n    assert(*gLocalVar() == 1);\n}\n\nint main(void) {\n    emulated_tls();\n    return 0;\n}\n```\n\n\u003e **cthreads.h** implements `thrd_local(type, variable)` macro, same behavior as above, but\n\u003e *will not emulate* if feature is *available* in your compiler, should be used for cross compatibility calling.\n\n## Emulate/create/control `Thread Local Storage` via macros\n\nThe design of these *macros* follow normal behaviors when using `thread local storage`.\n\n### In `rpmalloc.h`\n\nThe macro `tls_storage_extern(type, variable)` make function *proto*, and macro `tls_storage(type, variable)` will create functions as:\n\n```h\nC_API int rpmalloc_variable_tls;\nC_API tls_storage_t rpmalloc_variable_tss;\nC_API type* variable(void);\nC_API void variable_delete(void);\n```\n\nThe macro `tls_storage(type, variable)` will create the functions using:\n\n```h\nC_API int rpmalloc_tls_create(tls_storage_t *key, tls_dtor_t dtor);\nC_API void rpmalloc_tls_delete(tls_storage_t key);\nC_API void *rpmalloc_tls_get(tls_storage_t key);\nC_API int rpmalloc_tls_set(tls_storage_t key, void *val);\n```\n\n### In `cthreads.h`\n\nThe macro `thrd_local_extern(type, variable)` make functions *proto*, and `thrd_local(type, variable)` create functions as:\n\n**If compiler support *native* `thread_local`.**\n\n```h\nC_API thread_local type* thrd_variable_tls;\nC_API void variable_del(void);\nC_API void variable_update(type* value);\nC_API bool is_variable_empty(void);\nC_API type* variable(void);\n```\n\nMacro `thrd_local(type, variable)` also add.\n\n```c\nstatic thread_local type thrd_variable_buffer;\n```\n\n**If emulating `thread_local` is *detected/required*, will use platform `thread library` routines.**\n\n```h\nC_API int thrd_variable_tls;\nC_API tss_t thrd_variable_tss;\nC_API void variable_del(void);\nC_API void variable_update(type* value);\nC_API bool is_variable_empty(void);\nC_API type* variable(void);\n```\n\nMacro `thrd_local(type, variable)` also add when *emulating*.\n\n```c\nstatic type thrd_variable_buffer;\n```\n\n- The macro `thrd_local_external(type, variable)` makes **non-pointer** *version* of above.\n- The macro `thrd_static(type, variable)` makes local **non-extern** static function *version* of above.\n\n## Installation\n\nAny **commit** with an **tag** is considered *stable* for **release** at that *version* point.\n\nIf there are no *binary* available for your platform under **Releases** then build using **cmake**,\nwhich produces **static** libraries by default.\n\nYou will need the *binary* stored under `built`, and `*.h` headers, or complete `include` *folder* if **Windows**.\n\n### Linux\n\n```shell\nmkdir build\ncd build\ncmake .. -DCMAKE_BUILD_TYPE=Debug/Release -D BUILD_TESTS=OFF -D BUILD_EXAMPLES=OFF # use to not build tests and examples\ncmake --build .\n```\n\n### Windows\n\n```shell\nmkdir build\ncd build\ncmake .. -D BUILD_TESTS=OFF -D BUILD_EXAMPLES=OFF # use to not build tests and examples\ncmake --build . --config Debug/Release\n```\n\n### As cmake project dependency\n\n\u003e For **CMake** versions earlier than `3.14`, see \u003chttps://cmake.org/cmake/help/v3.14/module/FetchContent.html\u003e\n\nAdd to **CMakeLists.txt**\n\n```c\nfind_package(cthreads QUIET)\nFetchContent_Declare(cthreads\n URL https://github.com/zelang-dev/c-threads/archive/refs/tags/1.0.0.zip\n URL_MD5 0f1c66f911f0912aa2a955a486952a8d\n)\nFetchContent_MakeAvailable(cthreads)\n\ntarget_include_directories(${PROJECT_NAME}\n  AFTER PUBLIC $\u003cBUILD_INTERFACE:${CTHREADS_INCLUDE_DIR}\u003e\n  $\u003cINSTALL_INTERFACE:${CTHREADS_INCLUDE_DIR}\u003e)\ntarget_link_libraries(${PROJECT_NAME} PUBLIC cthreads)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzelang-dev%2Fc-threads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzelang-dev%2Fc-threads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzelang-dev%2Fc-threads/lists"}