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'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.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.
j1elo|2 years ago
Started from learning about Protothreads (cooperative concurrency) as described by Adam Dunkels [1], and ended up devising a Task manager/runner library with a main loop, so multiple protothreads could be scheduled easily. At any given point in time, any of the scheduled tasks could be running or yielding on some continuation point.
There's some beauty in grasping these concepts from the ground up. Some time later I had to learn JavaScript, and the concept of async/await clicked so fast and nicely in my mind. Saving distances, the core idea is fundamentally the same, and that was enlightening (and fun!)
[1]: https://dunkels.com/adam/pt/
swingingFlyFish|2 years ago
nineteen999|2 years ago
JonChesterfield|2 years ago
C11 has a <threads> header that should be usable in place of the pthreads api.
LoganDark|2 years ago
I may play with the C11-standard `threads.h`, but note that it was not implemented by MSVC at all until quite recently: https://devblogs.microsoft.com/cppblog/c11-threads-in-visual...
Edit: Made a C11 threads.h implementation as well. https://github.com/rkaehn/cr_task.h/pull/3
LoganDark|2 years ago
Cieric|2 years ago
Cloudef|2 years ago
rad_gruchalski|2 years ago
o11c|2 years ago
That said, the API here is undocumented and the chosen names are very confusing (implementations do not do what I would expect functions of those names to do).
kccqzy|2 years ago
krackers|2 years ago
GCD is a lot more heavyweight though (but it brings a lot of niceties), whereas this is going to be much more portable.
crest|2 years ago
samatman|2 years ago
When I see "impressive thing in only a few lines of C", I think someone has invented a new way to crash my computer if I don't color inside invisible, underspecified lines.
What I'd want to see, if I were looking for a library like this, is tests. I see that you do have a test suite: that's good. I also see that it covers the basic functionality: that's a good start. There are a number of excellent tools for testing for the various woes which C code is prone to, consider integrating them.
C code doesn't have to be full of goblins which will venture forth at midnight and eat the candy out of your nose. But it should be suspected of such goblins until demonstrated otherwise. Few lines is actually good! And the test suite suggests you've implemented a good surface area here.
And 350 lines means you probably only need a couple thousand more to really, very thoroughly test the code. It will take some time but it's well worth it.
Arelius|2 years ago
I have used this sort of thing a lot though, and something I often find both essential, and overlooked is you really need something like `cr_task_sync_and_do_work` that isn't just entirely blocking on the current thread. Since as you build systems on this sort of API, you very quickly get bound by threads just stuck waiting on their children jobs.
Having said that, I realize that complicates the implementation quite a bit. The most elegant approach I have seen is one where you put tasks on fibers, and when you sync, instead of entering a sync loop, or waiting on a mutex, you instead suspend the fiber in the awaiting job, and put it back on the task queue. This was detailed by Naughty Dog in their Job System talk at GDC: https://www.gdcvault.com/play/1022186/Parallelizing-the-Naug...
Cloudef|2 years ago
gonzus|2 years ago
rkaehn|2 years ago
unknown|2 years ago
[deleted]
marmaduke|2 years ago
sakras|2 years ago
https://fzn.fr/readings/ppopp13.pdf
cryptonector|2 years ago
up2isomorphism|2 years ago
mm007emko|2 years ago
eqvinox|2 years ago
thenewwazoo|2 years ago
[0] https://en.wikipedia.org/wiki/Indentation_style#K&R_style
antirez|2 years ago
colejohnson66|2 years ago
[0]: https://softwareengineering.stackexchange.com/a/99546/54480
dc-programmer|2 years ago
rkaehn|2 years ago
salamander014|2 years ago
Sometimes, functions may call other functions in the same code file.
This required that functions be declared before they are referenced so C knew it existed.
You can also see this on lines 84-92.
hinkley|2 years ago
The were, however, the only people in the world with multiple monitors (outside of SGI).
unknown|2 years ago
[deleted]
lexicality|2 years ago
adamrezich|2 years ago