Search code examples
c++c++11memory-alignmentmemory-layout

Practical use cases for alignof and alignas C++ keywords


I just learnt about alignof and alignas C++ keywords but I can't think about any practical case where a developer would want to use these keywords.

Does somebody know any practical use case for these keywords?


Solution

  • A common use case for the alignas specifier is for the scenario where you want to pass multiple objects between different threads through a queue (e.g., an event or task queue) while avoiding false sharing. False sharing will result from having multiple threads competing for the same cache line when they are actually accessing different objects. It is usually undesirable due to performance degradation.

    For example – assuming that the cache line size is 64 bytes – given the following Event class:

    struct Event {
       int event_type_;
    };
    

    The alignment of Event will correspond to the alignment of its data member, event_type_. Assuming that the alignment of int is 4 bytes (i.e., alignof(int) evaluates to 4), then up to 16 Event objects can fit into a single cache line. So, if you have a queue like:

    std::queue<Event> eventQueue;
    

    Where one thread pushes events into the back of the queue, and another thread pulls events from the front, we may have both threads competing for the same cache line. However, by properly using the alignas specifier on Event:

    struct alignas(64) Event {
       int event_type_;
    };
    

    This way, an Event object will always be aligned on a cache line boundary so that a cache line will contain an Event object at most. Therefore two or more threads will never be competing for the same cache line when accessing distinct Event objects (if multiple threads are accessing the same Event object, they will obviously compete for the same cache line).