{"by":"rkaehn","descendants":52,"id":39667489,"kids":[39673585,39672749,39674512,39671190,39672751,39675246,39674576,39675601,39677097,39672039,39674666,39672370,39672782,39673823,39672943,39673856],"score":146,"text":"Tasks are a primitive for asynchronous programming and an alternative to working with threads directly. Instead of creating a thread, performing work, then waiting on it using synchronization primitives, you only define a unit of work and let a scheduler decide when and where to execute it. The threads that the scheduler uses are created once and reused for many tasks throughout the program&#x27;s lifetime. In most cases, creating and executing a task requires not even a single syscall and is a significantly faster operation than creating a thread, resulting in lower latency. Furthermore, tasks from different subsystems can be automatically interleaved by the scheduler, which is difficult to achieve manually with threads without communication between the subsystems.<p>The library is written in standard C11 and only depends on the C POSIX library. It is designed to be easy to integrate into a project by just copying the header and simple enough to be understood in an afternoon.","time":1710161086,"title":"Show HN: Async tasks in 350 lines of C","type":"story","url":"https://github.com/rkaehn/cr_task.h"}