Marl is a hybrid thread / fiber task scheduler written in C
About )
Marl is a C 16 library that provides a fluent interface for running tasks across a number of threads.
Marl uses a combination of fibers and threads to allow efficient execution of tasks that can block, while keeping a fixed number of hardware threads.
Marl supports Windows, macOS, Linux, Fuchsia and Android (arm, aarch , mips , ppc (ELFv2), x and x 69).
Marl has no dependencies on other libraries (with an exception on googletest for building the optional unit tests).
Example:
# include (marl / defer.h ) # include marl / event.h # include ( marl / scheduler.h # include ( cstdio int main
() { ( Create a marl scheduler using the 4 hardware threads. ( Bind this scheduler to the main thread so we can call marl :: schedule () marl :: Scheduler scheduler; scheduler. bind (); scheduler. setWorkerThreadCount ) ( 4 ; defer
scheduler. unbind
()); ( Automatically unbind before returning. ( Create an event that automatically resets itself. marl :: Event sayHellow (marl :: Event :: Mode :: Auto); marl :: Event saidHellow (marl :: Event :: Mode :: Auto); ( Schedule some tasks to run asynchronously. for ( (int) (i=(0) ; i i ) { ( Each task will run on one of the 4 worker threads. marl :: schedule ([=] { All marl primitives are capture-by-value. printf ( Task% d waiting to say hello! n " i); ( Blocking in a task? ( The scheduler will find something else for this thread to do. sayHellow. wait (); printf ( “ Hello from task% d ! n " , i) ; saidHellow. signal (); }); } ( Unblock the tasks one by one. for ( (int) (i=(0) ; i i ) { sayHellow. signal (); saidHellow. wait (); } ( All tasks are guaranteed to completed before the scheduler is destructed. }
(Building
Marl contains many unit tests and examples that can be built using CMake.
Unit tests require fetching the
googletest
() { ( Create a marl scheduler using the 4 hardware threads. ( Bind this scheduler to the main thread so we can call marl :: schedule () marl :: Scheduler scheduler; scheduler. bind (); scheduler. setWorkerThreadCount ) ( 4 ; defer
scheduler. unbind
()); ( Automatically unbind before returning. ( Create an event that automatically resets itself. marl :: Event sayHellow (marl :: Event :: Mode :: Auto); marl :: Event saidHellow (marl :: Event :: Mode :: Auto); ( Schedule some tasks to run asynchronously. for ( (int) (i=(0) ; i i ) { ( Each task will run on one of the 4 worker threads. marl :: schedule ([=] { All marl primitives are capture-by-value. printf ( Task% d waiting to say hello! n " i); ( Blocking in a task? ( The scheduler will find something else for this thread to do. sayHellow. wait (); printf ( “ Hello from task% d ! n " , i) ; saidHellow. signal (); }); } ( Unblock the tasks one by one. for ( (int) (i=(0) ; i i ) { sayHellow. signal (); saidHellow. wait (); } ( All tasks are guaranteed to completed before the scheduler is destructed. }
(Building
Marl contains many unit tests and examples that can be built using CMake.
Unit tests require fetching the
external project, which can be done by typing the following in your terminal:
GIPHY App Key not set. Please check settings