https://github.com/rkaehn/cr_task.h Skip to content Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rkaehn / cr_task.h Public * Notifications * Fork 0 * Star 89 * Header-only library for asynchronous tasks in C License MIT license 89 stars 0 forks Branches Tags Activity Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Security * Insights rkaehn/cr_task.h This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main BranchesTags Go to file Code Folders and files Name Name Last commit message Last commit date Latest commit History 1 Commits .gitignore .gitignore LICENSE LICENSE README.md README.md cr_task.h cr_task.h test.c test.c View all files Repository files navigation * README * MIT license cr_task.h This library provides a minimal set of types and functions for an asynchronous task system in C. It was designed to be lock-free in the common case, with locks only needed for allocations of backing memory for the task pool and when the worker threads are starved for tasks to execute. It is written in standard C11 with no dependencies besides the C POSIX library. To get started, create an executor with the desired number of worker threads and define a task. cr_executor_t* exec = cr_executor_create(4); cr_task_t* task = cr_task_create(exec, func, args); cr_task_run(task); By default, the task is going to run immediately on cr_task_run. If you want it to run later, call cr_task_wait before run, and cr_task_signal later. cr_task_t* task = cr_task_create(exec, func, args); cr_task_wait(task); cr_task_run(task); // Later... (possibly on a different thread or inside a task) cr_task_signal(task); Instead of calling signal yourself, you can also request a signal from another task, creating a dependency. Here, task_1 has to finish before task_2 can start. cr_task_t* task_1 = cr_task_create(exec, func, args); cr_task_t* task_2 = cr_task_create(exec, func, args); cr_task_wait(task_2); cr_task_request_signal(task_2, task_1); cr_task_run(task_2); // task_2 does not execute yet cr_task_run(task_1); // task_1 executes, then task_2 Since telling one task to wait and requesting a signal from another is such a common operation, there is a shorthand function called cr_task_wait_request_signal. wait and signal calls must always be balanced, and as soon as the wait count hits zero and run has been called, the task executes. cr_task_t* task = cr_task_create(exec, func, args); cr_task_wait_request_signal(task, task_dep_1); cr_task_wait_request_signal(task, task_dep_2); cr_task_run(task); // task_dep_1 and task_dep_2 execute, then task When you want to synchronously wait for a task to finish execution, call cr_task_sync. Since a task destroys itself automatically after it has run, you need to call cr_task_retain before, and cr_task_release after waiting. And again, because this pattern of retain, run, sync, and release is relatively common, there is a shorthand called cr_task_run_sync. cr_task_t* task = cr_task_create(exec, func, args); cr_task_retain(task); cr_task_run(task); cr_task_sync(task); cr_task_release(task); // Or the shorthand... cr_task_run_sync(task); About Header-only library for asynchronous tasks in C Resources Readme License MIT license Activity Stars 89 stars Watchers 2 watching Forks 0 forks Report repository Languages * C 100.0% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.