{"id":18037971,"url":"https://github.com/quantumsheep/worker-c","last_synced_at":"2026-05-06T20:32:57.716Z","repository":{"id":96168326,"uuid":"163855142","full_name":"quantumsheep/worker-c","owner":"quantumsheep","description":"Multithreading done easy in C","archived":false,"fork":false,"pushed_at":"2019-01-29T15:41:45.000Z","size":26,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-10T08:47:57.570Z","etag":null,"topics":["c","linux","osx","posix","thread","windows","worker"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/quantumsheep.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":"2019-01-02T15:02:58.000Z","updated_at":"2023-09-08T17:48:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"f88695ec-82df-4755-94b0-82242f426e2f","html_url":"https://github.com/quantumsheep/worker-c","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/quantumsheep%2Fworker-c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantumsheep%2Fworker-c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantumsheep%2Fworker-c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantumsheep%2Fworker-c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quantumsheep","download_url":"https://codeload.github.com/quantumsheep/worker-c/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247268028,"owners_count":20911067,"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":["c","linux","osx","posix","thread","windows","worker"],"created_at":"2024-10-30T13:14:30.846Z","updated_at":"2026-05-06T20:32:52.692Z","avatar_url":"https://github.com/quantumsheep.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Worker library\nMultithreading can be very complicated in C, POSIX and Windows multithreading functions are not the same for instance.\nThis library was made in order to palliate this issue and facilitate the use and creation of new threads in C.\n\n\n# How to install\nJust drop `worker.h` and `worker.c` in your project and include `worker.h` using a relative path.\n\nSometimes you will need to add `-pthread` in gcc options (only if not on Windows).\n\n# Usage\n## worker()\n```c\nWorkerId\nworker(void *f, void *args, WorkerErr *err);\n```\nThis function is used to start any functions as a new thread. \n\n### Parameters\n**f**: the function that will be run as a new thread.  \n**args**: arguments to be passed to the given function.  \n**err**: the error code number, literally an `unsigned long`. It can be `NULL` but if not, see [`pthread_create` POSIX information page](http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html) for Linux or OSX and [`GetLastError()` documentation](https://msdn.microsoft.com/fr-fr/d852e148-985c-416f-a5a7-27b6914b45d4) for Windows.\n\n### Return value\nIt returns a `WorkerId` which is a `pthread_t` (`pthread.h`) on POSIX operating systems and a `HANDLE` (`windows.h`) on Windows.\nThe returned value can be `NULL` if their is an error.\n\n\n## worker_wait()\n```c\nWorkerErr\nworker_wait(WorkerId thread);\n```\nThis function is used to wait for a thread to end before continuing the program.\n\n### Parameters\n**thread**: a `WorkerId`, returned by the [`worker()`](#worker) function.\n\n### Return value\nIt's the error code number, literally an `unsigned long`. It can be `NULL` but if not, see [`pthread_create` POSIX information page](http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html) for Linux or OSX and [`GetLastError()` documentation](https://msdn.microsoft.com/fr-fr/d852e148-985c-416f-a5a7-27b6914b45d4) for Windows.\n\n\n## worker_stop()\n```c\nWorkerErr\nworker_stop(WorkerId thread);\n```\nTerminate a thread. Considered as an unsafe instruction.\n\n### Parameters\n**thread**: a `WorkerId`, returned by the [`worker()`](#worker) function.\n\n### Return value\nIt's the error code number, literally an `unsigned long`. It can be `NULL` but if not, see [`pthread_create` POSIX information page](http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html) for Linux or OSX and [`GetLastError()` documentation](https://msdn.microsoft.com/fr-fr/d852e148-985c-416f-a5a7-27b6914b45d4) for Windows.\n\n\n## Example\n```c\n#include \"worker/worker.h\"\n\n#include \u003cunistd.h\u003e\n#include \u003cstdio.h\u003e\n\nvoid\ntimeout(int time)\n{\n    sleep(time);\n    printf(\"Wake up\\n\");\n}\n\nvoid\nhandleError(WorkerErr err)\n{\n    if (err)\n    {\n        printf(\"Error code: %ld\\n\", err);\n        _exit(0);\n    }\n}\n\nint\nmain(int argc, char** argv)\n{\n    WorkerErr err;\n\n    void* args = { 2 };\n    WorkerId thread = worker(timeout, args, \u0026err);\n    handleError(err);\n\n    printf(\"The thread has started!\\n\");\n\n    err = worker_wait(thread);\n    handleError(err);\n\n    printf(\"Timeout finished.\\n\");\n}\n```\n\nThis example will print the following:\n```\nThe thread has started!\nWake up!\nTimeout finished\n```\n« `Wake up!` » printed 2 seconds after « `The thread has started!` » like wanted with the `sleep(2)`. And « `Timeout finished.` » directly printed after the thread's end.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantumsheep%2Fworker-c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquantumsheep%2Fworker-c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantumsheep%2Fworker-c/lists"}