Serializer Struct Reference

#include <serializer.h>

Inheritance diagram for Serializer:

Detailed Description

A Serializer is similar to a mutex (e.g. Spinlock) and guarantees mutually exclusive access to a shared resource. It has the benefit of better cache utilization because requests from different threads can be combined/aggregated and executed by the thread which first accessed the shared resource. Furthermore the code for your critical section can be executed asynchronously if you don't have to wait for it to finish.

Suppose you have a shared resource you can safely access it in the following way:

Serializer s; // Serializer to protect your shared resource
s.EnqueueAndWait(
()[]
{
... access shared resource ...
});

The equivalent code for a Spinlock/ScopedLock looks like this:

Spinlock s; // Spinlock to protect your shared resource
{
ScopedLock lock(s);
... access shared resource ...
}

The code for critical sections is executed in order of the enqueues: If lambda A was enqueued before lambda B it will be executed before it, the order of execution will never be changed.

Public Member Functions

template<typename FN >
void EnqueueAndWait (FN fn)
 
template<typename FN >
void Enqueue (FN fn)
 

Member Function Documentation

◆ EnqueueAndWait()

void EnqueueAndWait ( FN  fn)

Enqueues a lambda or object with operator () and waits until it has finished. The callable must not return a value, the Serializer is low level and does not support error handling via Result<>.

Parameters
[in]fnCallable object.

◆ Enqueue()

void Enqueue ( FN  fn)

Enqueues a lambda or object with operator (). This means the lambda might be asynchronously executed by a different thread and this method most likely returns before the lambda has been executed. The callable must not return a value, the Serializer is low level and does not support error handling via Result<>.

Parameters
[in]fnCallable object.