Author image

Thread Pool


A thread pool is a software design pattern that helps programmers achieve concurrency in an optimal way. By maintaining a pool of threads ready to execute at any moment performance is increased since latency is minimized by keeping a set of threads always available; at the ready, instead of frequently starting them up and destroying them because of frequent brief tasks. Thus we avoid short lived threads which break concurrency.

All this because creating and destroying threads takes a longer time than keeping them constantly alive and waiting.

It stands to reason for me to want to make a thread pool to test my understanding of various modern C++ features and multithreading, as well as have it adorn various other projects that could certainly benefit from its features.

Design

  • there is an array of threads - m_pool
  • there is a FIFO queue of tasks (a Task should be a...

Author image

Observer Design Pattern


Observer is a very important design pattern which is being used in almost every single self-respecting codebase as a way to efficienty communicate between objects in an event-based function. It wouldn't be an understatement to say it is the foundation of event based programming.

Underlying the popular, ever-present in networking, “Model View Controller” (MVC) design pattern, is the observer pattern.

Observers are used in every single game engine - they're known there mostly as “Event dispatchers”. This includes Unreal Engine.

The pattern promotes loose coupling since the Subjects don't need to know anything about the observers.

Alternative terminology for this pattern includes the following: - Subscribe-Publish - Listener-Publisher - Delegate-Model - Sink-Source, where the Subject is the source of events and the Observers are sinks of events

Design

  • a class,...

Author image

Command Design Pattern


Definition:
Encapsulate a request as an object, thereby letting users parameterize clients with different requests, queue or log requests, and support undoable operations.

Breakdown:
You are not encapsulating the object that sends the request, or the object that receives the request, but encapsulate the actual request as an object with state. Of course, some commands can be stateless chunks of pure behavior (no data members).
The command pattern also makes undoable operations a piece of cake (they're considerably harder to implement wihout it). Derived Commands can leverage CRTP to be statically bound. To familiarize yourself with CRTP check this tutorial.

Note that the code executing the commands is responsible for freeing their memory.

Design

  • define a base class or interface (abstract class)...

Author image

Curiously Recurring Template Pattern


Curiously Recurring Template Pattern, or CRTP for short, is ultimately a C++ only idiom. It is also known as Static Polymorphism or Simulated polymorphism.

Requirements for dynamic polymorphism:

  1. Is-a relationship between base and derived class.
  2. Base class defines a 'generic' algorithm that is used by derived class.
  3. The 'generic' algorithm is customized by the derived class.

Requirements for static polymorphism:

  1. 'Is-a' relationship between base and derived class.
  2. Base class defines a 'generic' algorithm that is used by derived class.
  3. The 'generic' algorithm is customized by the derived class.
  4. This method is pure virtual. The derived method implements it. But, to avoid run time costs:
  5. When the specialized method is invariant of the one in the base method then this resolution can be achieved...

1