<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gerardo Puga</title>
    <description>The latest articles on DEV Community by Gerardo Puga (@glpuga).</description>
    <link>https://dev.to/glpuga</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F403526%2F970aa96d-d3c4-4ca6-b790-3655a7c76acd.jpg</url>
      <title>DEV Community: Gerardo Puga</title>
      <link>https://dev.to/glpuga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/glpuga"/>
    <language>en</language>
    <item>
      <title>C++ lambdas, for beginners</title>
      <dc:creator>Gerardo Puga</dc:creator>
      <pubDate>Mon, 14 Sep 2020 00:09:54 +0000</pubDate>
      <link>https://dev.to/glpuga/c-lambdas-for-beginners-313c</link>
      <guid>https://dev.to/glpuga/c-lambdas-for-beginners-313c</guid>
      <description>&lt;ul&gt;
&lt;li&gt;What's this all about&lt;/li&gt;
&lt;li&gt;A Lambda's full name&lt;/li&gt;
&lt;li&gt;
Analysis of the lambda expression

&lt;ul&gt;
&lt;li&gt;The return type of the lambda&lt;/li&gt;
&lt;li&gt;The capture list&lt;/li&gt;
&lt;li&gt;Mutable lambdas&lt;/li&gt;
&lt;li&gt;Generalized captures (#C++14 and beyond)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;What can and cannot be captured&lt;/li&gt;
&lt;li&gt;Captures and the &lt;code&gt;this&lt;/code&gt; pointer&lt;/li&gt;
&lt;li&gt;An intuition of the closure class&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What this all about  &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;When C++11 came out it came with a lot of goodies, and one of the best additions to the language repertoire were lambda functions.&lt;/p&gt;

&lt;p&gt;Most people's first exposure to lambda functions in C++ is in the way of "throwaway functions" scattered around the code, because that's what lambdas are used for most of the time. Take for instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto addition = [](int x, int y) { return x + y; };
std::cout &amp;lt;&amp;lt; addition(4, 3) &amp;lt;&amp;lt; std::endl;
std::cout &amp;lt;&amp;lt; addition(99, 1) &amp;lt;&amp;lt; std::endl;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here a lambda function is created to perform a simple addition between two numbers, and then the lambda gets called a couple of times with different input arguments each time. When execution exits the scope where the lambda object was created, the lambda will be destroyed just like any other local variable.&lt;/p&gt;

&lt;p&gt;Lambdas can do more than that, though. Lambdas are executable objects, and just like other objects, they can be moved around, copied, stored in containers, passed as arguments, etc. Lambdas can carry a context with them, so that they can be created in one place and transferred to another for execution. Lambdas can be stateful, and can also be linked to the state of an external object.&lt;/p&gt;

&lt;p&gt;It must be said that lambdas don't really bring anything radically new to the C++ table. Everything that can be done with lambdas today could already be done with pre-C++11 versions of C++ using good-old functor objects (objects that can be called like functions), if you bothered to do the effort to write them.&lt;/p&gt;

&lt;p&gt;The real advantage of the lambda notation, though, is that it provides a compact way to describe an executable object in such a way that &lt;strong&gt;the compiler can do the implementation for us&lt;/strong&gt;. The programmer can then focus on the &lt;em&gt;what&lt;/em&gt; instead of on the &lt;em&gt;how&lt;/em&gt;, and explore design possibilities that might have been avoided otherwise&lt;/p&gt;

&lt;p&gt;During the rest of the article I'll try to introduce you to how lambda functions get declared, and what the ins and outs of variable capture are. &lt;/p&gt;

&lt;p&gt;Please notice that the article is aimed at beginners trying to "get it" and/or intermediate users looking for a refresh. If you already "get" lambdas, you can look try the references in cite at the very end for better material.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Lambda's full name &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Lets start by getting semantics out of the way. &lt;/p&gt;

&lt;p&gt;In reality, there's no such thing as a lambda. "Lambda" is just a colloquial term to that, depending on the context, will usually refer to one of three things.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;lambda expression&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;closure object&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;closure class&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three of them are present in the following sentence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto addition = [](int x, int y) { return x + y; };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[](int x, int y) { return x + y; }&lt;/code&gt; is the lambda expression.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;addition&lt;/code&gt; is the closure object.&lt;/li&gt;
&lt;li&gt;You can't see the closure class, but it's there in the form of the type of the closure object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever you hear someone talking about a lambda, they are most likely talking about one of the three above, and in most cases it'll be one of the first two (the third one gets far less press time).&lt;/p&gt;

&lt;p&gt;I'll keep it light and talk about &lt;code&gt;lambdas&lt;/code&gt; when it suits me better, but I'll use the terms above when the distinction is important.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis of the lambda expression &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The lambda expression is the most visible expression of the lambda in the code.&lt;/p&gt;

&lt;p&gt;The structure of the lambda expression is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[...capture list...](...parameters...) -&amp;gt; &amp;lt;return type&amp;gt; { ... body ... }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;but in practice, it will be a lot simpler than this. In fact, the humblest of all lambda expressions is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[]{}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;which defines a lambda that defines captures nothing, takes in nothing as parameters, does nothing, and returns no data; that lambda is, in other words, equivalent to this function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void f() {}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Just like for function declarations, the code of the lambda is enclosed within the &lt;code&gt;{}&lt;/code&gt; brackets, and the input parameters within the &lt;code&gt;()&lt;/code&gt; pair. There's not a lot to say about either of these components of the lambda, since they follow both in form and in function from their equivalents on the function declaration.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;()&lt;/code&gt; of the parameters list can be completely omitted when the lambda is not meant to receive any parameters. That's why the humblest lambda is &lt;code&gt;[]{}&lt;/code&gt; and not &lt;code&gt;[](){}&lt;/code&gt;. If a return type is specified, however, the &lt;code&gt;()&lt;/code&gt; needs to be written even if the parameter list is empty.&lt;/p&gt;

&lt;p&gt;I want to talk at length about captures, which is what separates lambdas from just being throwaway functions, but allow me to get something out of the way first and discuss the return type of the lambda first.&lt;/p&gt;

&lt;h3&gt;
  
  
  The return type of the lambda &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The return value type of the lambda can be omitted from the expression, and most people will not write it. When the type is not present the compiler will infer it from the rest of the expression.&lt;/p&gt;

&lt;p&gt;I won't be writing the return type in the rest of the examples in this articles unless I really need it. That's why I wanted to task about this early: it's less typing for me.&lt;/p&gt;

&lt;p&gt;Only in a minority of cases you'll see lambda expression that fully state the return types of the lambda. In those cases it will usually be because of one of two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The lambda body is such that the return type is ambiguous to the compiler.&lt;/li&gt;
&lt;li&gt;The coder wants to coerce the return type into something different from what the compiler would have inferred.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever the return type is not written in the lambda expression the compiler uses a simple but effective set of rules to determine what type &lt;em&gt;you meant&lt;/em&gt; to return:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there are no &lt;code&gt;return&lt;/code&gt; statements, or if the ones present are bare &lt;code&gt;returns&lt;/code&gt; with no return value (such as &lt;code&gt;return;&lt;/code&gt;), then the lambda is assumed to return &lt;code&gt;void&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If there's a single &lt;code&gt;return&lt;/code&gt; statement and it has a return expression, the return type will be the type that results from evaluating the expression.&lt;/li&gt;
&lt;li&gt;If there are multiple &lt;code&gt;return&lt;/code&gt; statements and all of them have a return expression that evaluates to the very same type, that will be the return type of the lambda.&lt;/li&gt;
&lt;li&gt;If none of the above is the case, the compiler will give up and then you'll have to explicitly state the return type.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A few examples:&lt;/p&gt;

&lt;p&gt;This declares a lambda that just returns &lt;code&gt;void&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto f1 = [](){};  // could be just []{}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the return expression evaluates to &lt;code&gt;int&lt;/code&gt;, and therefore the return type of the lambda will be the same type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto f3 = [](int n) { return n; };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The return type in the expression here evaluates to &lt;code&gt;bool&lt;/code&gt;, so that'll be the type of the return value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto f4 = [](int n) { return n &amp;gt; 5; };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are two return expressions in the following example, but both are &lt;code&gt;bool&lt;/code&gt;. Since there's no ambiguity the return type will be &lt;code&gt;bool&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto f5 = [](int n) {
  if (n == 5) {
      return true;
  } else {
      return false;
  }
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case there are two return statements with expressions that evaluate to different types (&lt;code&gt;bool&lt;/code&gt; and &lt;code&gt;int&lt;/code&gt;). In this case the programmer needs to state state the return type explicitly, because it's not possible for the compiler to decide what the return type should be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto f6 = [](int n) -&amp;gt; bool {                
  if (n &amp;lt; 0) {
      return false;
  } else {
      return n; /* returns true if n &amp;gt; 0 */
  }
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case we state the return value to coerce the return type to be a &lt;code&gt;bool&lt;/code&gt; instead of an &lt;code&gt;int&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto f7 = [](const std::vector&amp;amp; n) -&amp;gt; bool { return v.size(); };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  The capture list &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The capture list of a lambda is a list of variable names &lt;em&gt;in the enclosing scope of the lambda&lt;/em&gt; that have to be &lt;em&gt;captured&lt;/em&gt; to become accessible within the lambda. &lt;/p&gt;

&lt;p&gt;In a lambda expression the capture list is written between the square brackets &lt;code&gt;[]&lt;/code&gt;. It is not optional, and the brackets must be present even if the capture list is empty.&lt;/p&gt;

&lt;p&gt;A capture creates a variable within the scope of the lambda body that has the same name as the variable being captured, and which can be (depending on the mode of capture) either &lt;strong&gt;a copy&lt;/strong&gt; of the external variable, or &lt;strong&gt;a reference&lt;/strong&gt; to it.&lt;/p&gt;

&lt;p&gt;Captures that generate a copy of the external variable will be called &lt;em&gt;by-copy&lt;/em&gt; captures, while the ones that generate a reference to an external variable will be called &lt;em&gt;by-reference&lt;/em&gt; captures.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;em&gt;by-copy&lt;/em&gt; capture creates a variable within the scope of the lambda that is a copy of the variable being captured, with the same value the later had at the time the lambda was created. By-copy captures are read-only, unless the lambda is &lt;em&gt;mutable&lt;/em&gt; (more on that later).&lt;/li&gt;
&lt;li&gt;A &lt;em&gt;by-reference&lt;/em&gt; capture stores a reference to the variable being captured, which can be used to read or update the value of the external variable at any time during the lifetime of the lambda.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To ground those definitions with an example, take a look at the code fragment below. In it two local variables get defined, followed by the definition of a lambda function that captures them both.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int foo = 33;
int bar = 22;
auto within_range = [foo, &amp;amp;bar](int n) {
  return foo + bar;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The capture list in the lambda expression captures both variables: &lt;code&gt;foo&lt;/code&gt; is captured by-copy, while &lt;code&gt;bar&lt;/code&gt; is captured by-reference.&lt;/p&gt;

&lt;p&gt;It's important to realize that that while the names of the captured variables are the same as the names of the external variables that they mirror, they are different variables. i.e. &lt;strong&gt;the &lt;code&gt;foo&lt;/code&gt; variable within the body of the lambda is not the same &lt;code&gt;foo&lt;/code&gt; outside.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you probably guessed from the example, the capture mode is declared by prefixing by-reference captures with &lt;code&gt;&amp;amp;&lt;/code&gt;. By-copy captures, on the other hand, have no prefix at all.&lt;/p&gt;

&lt;p&gt;Additionally, there are two  default capture modes that allow us to capture every variable that is used in the body and not explicitly mentioned in the capture list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A bare &lt;code&gt;&amp;amp;&lt;/code&gt; will capture by-reference anything used but not explicitly captured by-copy.&lt;/li&gt;
&lt;li&gt;A bare &lt;code&gt;=&lt;/code&gt; will capture by-copy anything used but not explicitly captured by-reference.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a typical capture list you'll find a mixture of default capture modes with named captures. There are a few rules to this mix, however. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The default modes &lt;code&gt;=&lt;/code&gt; and &lt;code&gt;&amp;amp;&lt;/code&gt; cannot be both present.&lt;/li&gt;
&lt;li&gt;If a default mode is present, it must lead the list.&lt;/li&gt;
&lt;li&gt;If a &lt;code&gt;=&lt;/code&gt; default capture is present, any named capture that follows must be by-reference.&lt;/li&gt;
&lt;li&gt;If a &lt;code&gt;&amp;amp;&lt;/code&gt; default capture is present, any named capture that follows must be by-copy.
The rules make sense if you think about it, so it's not really necessary to worry about them too much.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are some examples of typical capture lists:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;[]&lt;/strong&gt; Empty capture list, nothing will be captured.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[foo]&lt;/strong&gt; Capture &lt;code&gt;foo&lt;/code&gt; by copy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[&amp;amp;bar]&lt;/strong&gt; Capture &lt;code&gt;bar&lt;/code&gt; by reference.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[foo, &amp;amp;bar]&lt;/strong&gt; Capture &lt;code&gt;foo&lt;/code&gt; by-copy and &lt;code&gt;bar&lt;/code&gt; by-reference.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[=]&lt;/strong&gt; Capture anything named from the enclosing scope by-copy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[&amp;amp;]&lt;/strong&gt; Capture anything named from the enclosing scope by-reference.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[&amp;amp;, foo, bar]&lt;/strong&gt; Capture anything named from the enclosing scope by reference, except &lt;code&gt;foo&lt;/code&gt; and &lt;code&gt;bar&lt;/code&gt; which must be captured by-copy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;[=, &amp;amp;foo]&lt;/strong&gt; Capture anything named from the enclosing scope by copy, except &lt;code&gt;foo&lt;/code&gt; which must be captured by-reference.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Mutable lambdas &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;By default &lt;em&gt;by-copy&lt;/em&gt; captures are not writable, and therefore the following fragment is an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int value;
auto bad_lambda = [value]() { value += 10; };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By-copy captures can be made writable if the lambda is declared as &lt;code&gt;mutable&lt;/code&gt;. This makes the lambda stateful: any change you do to a by-copy capture will be carried over to the next execution of the same lambda.&lt;/p&gt;

&lt;p&gt;For example, in this example the lambda will &lt;em&gt;remember&lt;/em&gt; any update to the value of the captured &lt;code&gt;initial_value&lt;/code&gt; variable. The value of the external variable, however, will remain unchanged because the lambda updates a copy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int initial_value{5};

auto counter_lambda = [initial_value]() mutable {
    std::cout &amp;lt;&amp;lt; initial_value++ &amp;lt;&amp;lt; std::endl;
};

// each call will increment the internal copy
// stored within the lambda, and change carry over to 
// the next call.
counter_lambda(); // will print 5
counter_lambda(); // will print 6
counter_lambda(); // will print 7

// the original variable outside of the lambda is unchanged
std::cout &amp;lt;&amp;lt; initial_value &amp;lt;&amp;lt; std::endl;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;by-reference&lt;/em&gt; captures, on the other hand, can be both read and written regardless of whether the lambda is &lt;code&gt;mutable&lt;/code&gt; or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int total{0};

auto accumulate = [&amp;amp;total](int n) { total += n; };

// each call updates the value of the references variable
accumulate(1);
accumulate(2);
accumulate(3);

// print the accumulated value, 6
std::cout &amp;lt;&amp;lt; total &amp;lt;&amp;lt; std::endl;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Generalized captures (C++14 and beyond) &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;All the talk so far has been about captures as they were introduced when C++11 came out.&lt;/p&gt;

&lt;p&gt;These captures work great, and there's a lot you can do with them, but after a while you'll notice that there are a couple of ways in which they fall short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A capture always has the same name as the variable that was captured. This is not a big deal, of course, but sometimes you'd like to be able to name them something else.&lt;/li&gt;
&lt;li&gt;To capture a value it needs to be previously stored in a variable; it's not possible to capture the result of an expression.&lt;/li&gt;
&lt;li&gt;You can't use move semantics with captures. Captured objects need to be copyable; if they are not then you'll need to capture them by-reference, which may create a problem of ownership, or you'll need to do some other trick. This may be particularly annoying if you use &lt;code&gt;unique_ptr&lt;/code&gt; a lot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To mend this, C++14 upgraded lambdas with &lt;code&gt;generalized lambda captures&lt;/code&gt; which &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;allow you to name the internal name of the capture anything you like.&lt;/li&gt;
&lt;li&gt;allow you to capture not only variables, but also the result of expressions (only by-copy).&lt;/li&gt;
&lt;li&gt;more importantly, allow you to capture move-only variables like &lt;code&gt;unique_ptr&lt;/code&gt; instances.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The price to pay for these welcome improvements is that generalized captures are bit more verbose than regular captures because you need to state both the name for the variable being captured, and the name of the capture variable created within the lambda. The syntax is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;internal_name=expression&lt;/code&gt; for by-copy captures.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;internal_name=external_name&lt;/code&gt; for by-reference captures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, this example uses generalized captures to capture &lt;code&gt;counter&lt;/code&gt; by reference (naming it &lt;code&gt;cnt&lt;/code&gt; within the lambda), and also captures the result of &lt;code&gt;3 * mean_level&lt;/code&gt; by copy (naming the result &lt;code&gt;limit&lt;/code&gt; within the lambda).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int mean_level{5};
int counter{0};

auto f = [&amp;amp;cnt = counter, limit = 3 * mean_level]() {
  if (cnt &amp;lt; limit) cnt++;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To capture a move-only object, you just need to make sure the right side of the by-copy assignment is an rvalue, which can be done by providing a temporary value, or by using &lt;code&gt;std::move&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto adapter = std::make_unique&amp;lt;Adapter&amp;gt;();

auto runner = [adapter = std::move(adapter)]() { adapter-&amp;gt;run(); }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The extra verbosity of generalized lambda captures is a very small price to pay given that you're not even required to pay for it: you can still use regular C++11 captures when that suits you better, and mix generalized and regular captures to get the best of each:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto f = [&amp;amp;counter, limit = 3 * mean_level]() {
  if (counter &amp;lt; limit) counter++;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  What can and cannot be captured &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Earlier I said that only variables in the immediate local scope of the lambda can be captured. I mentioned it only in passing, and it probably flew under the radar when I said it.&lt;/p&gt;

&lt;p&gt;However, this not a minor detail or a technicality, and to see why lets see what &lt;strong&gt;cannot&lt;/strong&gt; be captured.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global scope variables and static data members can't be captured.&lt;/li&gt;
&lt;li&gt;Non-static class members can't be captured directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first one may strike you as obvious if you think of it, since globals are accessible from within any function, and lambdas are function-like ("callable") objects, there's no reason for them to be an exception to this.&lt;/p&gt;

&lt;p&gt;Still, you should keep in mind that globals must be regarded within a lambda just like by-reference captures are. This may have important implications in multi-threaded programs.&lt;/p&gt;

&lt;p&gt;Static class members are just globals in disguise, so it's no surprise that as far as lambdas are concerned, they have the exact same restrictions.&lt;/p&gt;

&lt;p&gt;Non-static class members cannot be captured either, but here the truth is more nuanced: actually they can, kind-of, but they cannot be captured in the same sense in which captures work for regular local variables.&lt;/p&gt;

&lt;p&gt;Before we dig deeper into this, however, we need to take a short detour to talk about the pointer &lt;code&gt;this&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Captures and the &lt;code&gt;this&lt;/code&gt; pointer &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;During execution each non-static class method has access to an implicitly created &lt;code&gt;this&lt;/code&gt; pointer that references the instance on which the method was called. This is what gives methods access to non-static data members of the class.&lt;/p&gt;

&lt;p&gt;Typically you don't need to de-reference that pointer explicitly since the compiler will do it for you, but you can if want to be more explicit. For instance, in this fragment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Value {
 public:
  void set(const int x) { x_ = x; }
  int get() const { return x_; }
 private:
  int x_;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we can make the dependency on &lt;code&gt;this&lt;/code&gt; more visible by rewriting both methods as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void set(const int x) { this-&amp;gt;x_ = x; }
int get() const { return this-&amp;gt;x_; }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It's important to notice that this does not change the way the code is compiled, it only makes explicit what the compiler is doing behind your back. &lt;/p&gt;

&lt;p&gt;This detour to talk about &lt;code&gt;this&lt;/code&gt; (pun intended) is because now that we have unmasked how access to data members works it's easier to understand the &lt;em&gt;nuanced&lt;/em&gt; version of how non-static data member capture works.&lt;/p&gt;

&lt;p&gt;Non-static class members cannot be captured because they are not in the local scope around the lambda, they are in the class scope.&lt;/p&gt;

&lt;p&gt;However, the &lt;code&gt;this&lt;/code&gt; pointer can be captured, because &lt;em&gt;it is&lt;/em&gt; a variable within the immediate scope of lambdas that get created in non-static methods of a class! By capturing &lt;code&gt;this&lt;/code&gt; the lambda gets access to all non-static data members and also to instance methods.&lt;/p&gt;

&lt;p&gt;In order to capture this, you just add it to the capture list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto is_empty = [this]() { return queue_.empty(); } 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It's important to not that &lt;code&gt;this&lt;/code&gt; is not a regular variable, and it's nature imposes a limitation to the capture process: &lt;code&gt;this&lt;/code&gt; can only be captured &lt;em&gt;by-copy&lt;/em&gt;; trying to capture the pointer like in &lt;code&gt;[&amp;amp;this]&lt;/code&gt; is syntactic error. Default capture modes will also capture &lt;code&gt;this&lt;/code&gt;, but notice that even if &lt;code&gt;this&lt;/code&gt; is captured by the &lt;code&gt;&amp;amp;&lt;/code&gt; default capture mode, the pointer will still be copied.&lt;/p&gt;

&lt;p&gt;Now, here comes the catch: remember that &lt;code&gt;this&lt;/code&gt; is not the instance, &lt;code&gt;this&lt;/code&gt; is a pointer to the instance. You're not capturing the instance by copy, your only copying a pointer to it. &lt;/p&gt;

&lt;p&gt;This is really important, because it means that any access to the instance members are still &lt;em&gt;reference-like&lt;/em&gt;: by de-referencing &lt;code&gt;this&lt;/code&gt; the lambda is accessing the original external variables, not copies of them!&lt;/p&gt;

&lt;p&gt;This is the reason &lt;a href="https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture"&gt;some sources&lt;/a&gt; explain the capture &lt;code&gt;[this]&lt;/code&gt; as capturing &lt;em&gt;the object by reference&lt;/em&gt;. It's not exactly true, but it's close enough.&lt;/p&gt;

&lt;p&gt;Now, this detail can bite you hard, especially if you fall for thinking that &lt;code&gt;[=]&lt;/code&gt; means that "everything gets captured by-copy", as in the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;functional&amp;gt;
#include &amp;lt;string&amp;gt;

using namespace std;
using Filter = std::function&amp;lt;bool(const std::string &amp;amp;)&amp;gt;;

class FilterFactory {
 public:
  Filter buildFilter(const string &amp;amp;name) {
    name_ = name;
    return [=](const std::string &amp;amp;name) { 
        return (name == name_);
    };
  }
 private:
  std::string name_;
};

int main() {
    FilterFactory factory;
    auto filter_adam = factory.buildFilter("adam");
    auto filter_eva  = factory.buildFilter("eva");
    cout &amp;lt;&amp;lt; filter_adam("adam") &amp;lt;&amp;lt; endl; // should have returned true, but returns false
    cout &amp;lt;&amp;lt; filter_adam("eva") &amp;lt;&amp;lt; endl;  // should have returned false, but returns true
    cout &amp;lt;&amp;lt; filter_eva("adam") &amp;lt;&amp;lt; endl;
    cout &amp;lt;&amp;lt; filter_eva("eva") &amp;lt;&amp;lt; endl;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the unsuspecting programmer may have expected &lt;code&gt;[=]&lt;/code&gt; to capture everything by-copy, including class members. That would have made each lambda completely self-contained and independent of the original factory instance.&lt;/p&gt;

&lt;p&gt;In reality, however, the default capture mode is not capturing &lt;code&gt;name_&lt;/code&gt; at all; it is &lt;code&gt;this&lt;/code&gt; that's getting captured, and the access to &lt;code&gt;name_&lt;/code&gt; is actually being done through &lt;code&gt;this-&amp;gt;name_&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code builds, but behavior is not what the coder expected. For all practical purposes &lt;code&gt;name_&lt;/code&gt; is being accessed by reference, allowing lambdas to "see" changes to the variable  value that are done after the creation of the lambda.&lt;/p&gt;

&lt;p&gt;And it gets worse: if the &lt;code&gt;factory&lt;/code&gt; variable is destroyed before the lambdas, the &lt;code&gt;this&lt;/code&gt; pointer stored in the lambdas becomes invalid and any access to the data members of the instance it used to point to becomes undefined behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  An intuition of the closure class &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Before closing the article I'll talk a bit about the closure class. The idea is not to be rigorous, but just to provide the reader with an intuition of how lambdas get implemented by the compiler under the hood.&lt;/p&gt;

&lt;p&gt;The first thing to state is that there's no single closure class. A custom closure class gets created automatically by the compiler for each lambda expression.&lt;/p&gt;

&lt;p&gt;These compiler-generated classes cannot be seen or changed, because only the compiler knows what they look like. That's why it is frequently said that the closure objects have an anonymous type.&lt;/p&gt;

&lt;p&gt;To take a peek under the hood we can implement our own closure class from the description in the lambda expression. Without loss of generality, lets say we were asked to compile a fragment like the one in the following fragment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int foo;
bool bar;

auto lf = [foo, &amp;amp;bar](int factor) { return foo * bar * factor; };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;From the expression we can deduce that upon construction the lambda needs to capture two variables, one by copy (an integer) and another by reference (a boolean). The lambda objects that get instantiated from the expression need to be callable with  a single input parameter (an integer) and must return a value after execution (another integer).&lt;/p&gt;

&lt;p&gt;A possible implementation of the closure type for the lambda in the example above would be the following one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ClosureType {
 public:
    ClosureType(int foo, double &amp;amp;bar) : foo_{foo}, bar_{bar} {}

    double operator()(int factor) const {
        return foo_ * bar_ * factor;
    }
  private:
   int foo_;
   double &amp;amp;bar_;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Where you can see that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Captures become closure class constructor parameters. &lt;/li&gt;
&lt;li&gt;By-copy captures like &lt;code&gt;foo&lt;/code&gt; are stored within member variables in each instance.&lt;/li&gt;
&lt;li&gt;By-reference captures like &lt;code&gt;&amp;amp;bar&lt;/code&gt;, are not stored themselves, but a reference to them is stored in the class.&lt;/li&gt;
&lt;li&gt;By overloading &lt;code&gt;operator()&lt;/code&gt; the closure objects produced by the class become callable objects.&lt;/li&gt;
&lt;li&gt;The code body, return type and parameter list of the lambda expression become the body, return type and parameter list of the &lt;code&gt;operator()&lt;/code&gt; overload.&lt;/li&gt;
&lt;li&gt;In  this case the &lt;code&gt;operator()&lt;/code&gt; overload is a &lt;code&gt;const&lt;/code&gt; method because the lambda is not &lt;code&gt;mutable&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the closure example above it is readily visible that captured variables get read on construction, while lambda parameters get passed to the lambda on execution.&lt;/p&gt;

&lt;p&gt;While not very rigorous, our example above is good enough to get an idea of what a closure class may look like and how each part of the expression affects the implementation of the lambda.&lt;/p&gt;

&lt;p&gt;The closure class example shows how the number and mode of captures impacts on the size of the closure objects that get instantiated from it: lambdas are more than just code (like a function would be), they carry a context with them, and the size of the context depends on the size and type of the captures.&lt;/p&gt;

&lt;p&gt;By-reference captures are light, and only add a pointer to the size of the lambda object, but they don't guarantee that the captured object will exist at least as long as the lambda exists. By-copy captures, on the other hand, do guarantee the lifetime, but they can be heavy to move around because of the copy operation. Moving around lambdas will be at least as expensive as the most expensive by-copy capture they own.&lt;/p&gt;

&lt;p&gt;In reality compilers can use a variety of implementations for lambdas depending on what's better suited for each case in particular. In particular, if a lambda does not capture any variable then it's usually cheaper to implement it with a simple anonymous function instead of an anonymous class; closure objects in this case become just pointers to that anonymous function.&lt;/p&gt;

&lt;p&gt;That's why no-capture lambdas can be assigned to variables of correct pointer-to-function type variable, but lambdas that capture variables cannot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int factor = 2;
auto no_capture_lambda = [](int n) { return 2 * n; }; 
auto with_capture_lambda = [factor](int n) { return factor * n; }; 

int (*f_ptr_1)(int) = no_capture_lambda;   // this is ok
int (*f_ptr_2)(int) = with_capture_lambda; // this fails to build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;There's more to say about lambdas, of course, but this is probably enough for a good first bite.&lt;/p&gt;

&lt;p&gt;For a deeper coverage you can visit the somewhat terse but extremely complete &lt;a href="https://en.cppreference.com/w/cpp/language/lambda"&gt;cppreference.com page on the topic&lt;/a&gt;. There you will find also lots of information on new developments in lambda functions in C++17 and beyond, which is something I intentionally left out.&lt;/p&gt;

&lt;p&gt;If you have access to a copy of &lt;a href="https://www.oreilly.com/library/view/effective-modern-c/9781491908419/"&gt;Effective Modern C++&lt;/a&gt;, go read it. If you don't, go get it. The chapter on lambdas is extremely well written and informative, just like the rest of the book. You'll find there a neat trick to use move semantics with lambdas when generalized captures are not available in your compiler. &lt;/p&gt;

&lt;p&gt;I did not get a lot into passing lambdas around, but I did try to put some emphasis on the problems caused by capturing stuff by reference, either intentionally or by mistake. Understanding that is extremely important to safely execute lambdas in a different context from the one they were created in, and to determine how they will interact with other objects in multi-threaded code.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this, until next time!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>cpp</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Multithreading by example, the stuff they didn't tell you</title>
      <dc:creator>Gerardo Puga</dc:creator>
      <pubDate>Thu, 20 Aug 2020 23:54:24 +0000</pubDate>
      <link>https://dev.to/glpuga/multithreading-by-example-the-stuff-they-didn-t-tell-you-4ed8</link>
      <guid>https://dev.to/glpuga/multithreading-by-example-the-stuff-they-didn-t-tell-you-4ed8</guid>
      <description>&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Filu4j5ea4se1p8ljpy56.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Filu4j5ea4se1p8ljpy56.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Concurrent code can be daunting. In the very moment when you trigger the creation of a second thread in your system you're opening the gates for a completely new type of bug to come crashing through the doors...&lt;/p&gt;

&lt;p&gt;And it's not just bugs of a &lt;em&gt;new kind&lt;/em&gt;, but bugs of the &lt;em&gt;worst kind&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They happen infrequently.&lt;/li&gt;
&lt;li&gt;They are strongly dependent on execution context (timing, load, etc.).&lt;/li&gt;
&lt;li&gt;When they happen, they are hard to reproduce, and hence to test for.&lt;/li&gt;
&lt;li&gt;Very often they are perfect examples of &lt;a href="https://en.wikipedia.org/wiki/Heisenbug" rel="noopener noreferrer"&gt;heisenbugs&lt;/a&gt;: they vanish or mutate as soon as you set your debug tools on them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea for this article came to me while implementing a very simple multi-threaded event queue, where one or more classes can post an events, and one or more independent subscribers can be notified of these events through a callback.&lt;/p&gt;

&lt;p&gt;My initial requirements were actually very relaxed, since the use case I needed to satisfy was a really tame one.&lt;/p&gt;

&lt;p&gt;And yet, as I created the implementation I realized that, simple as it was, the code was very good example of a large number of implementation issues that I often get asked about.&lt;/p&gt;

&lt;p&gt;So I decided I would write about it, because I'm lazy, and I'd like to avoid repeating myself in the future.&lt;/p&gt;

&lt;p&gt;This is what you can find in the rest of the article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;That's nice. Where's the code?&lt;/li&gt;
&lt;li&gt;
Unpacking the stuff

&lt;ul&gt;
&lt;li&gt;Event sources should be decoupled from subscribers&lt;/li&gt;
&lt;li&gt;Subscribers can be decoupled from each other, but I won't&lt;/li&gt;
&lt;li&gt;Identify the execution threads at play&lt;/li&gt;
&lt;li&gt;Before we move on: callbacks are evil&lt;/li&gt;
&lt;li&gt;volatile vs atomic&lt;/li&gt;
&lt;li&gt;What if I have lots of shared variables between threads?&lt;/li&gt;
&lt;li&gt;Callbacks, yes, but RAII-ish&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

The devil hides is in the details

&lt;ul&gt;
&lt;li&gt;The "lost event" pitfall&lt;/li&gt;
&lt;li&gt;The "deaf condition variable" pitfall (part I)&lt;/li&gt;
&lt;li&gt;The "deaf condition variable" pitfall (part II, the revenge)&lt;/li&gt;
&lt;li&gt;One more, for the road&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Overcoming concurrency's hardships

&lt;ul&gt;
&lt;li&gt;The solution: write bug-free concurrent code&lt;/li&gt;
&lt;li&gt;That's wishful thinking. What else have you got?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;That's it, the end&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  That's nice. Where's the code?  &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;This is the interface for the class this article will talk about, minus the boilerplate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EventQueue {
 public:
  void postEvent();
  CallbackHandler subscribeToEvent(const CallbackFunction &amp;amp;callback);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can find the rest of the code &lt;a href="https://github.com/glpuga/myEventQueue" rel="noopener noreferrer"&gt;in this repo&lt;/a&gt;. I'll be cutting an pasting interesting bits from it below as they get talked about.&lt;/p&gt;

&lt;p&gt;It's a publisher/subscriber architecture, where one or more clients subscribe to an &lt;strong&gt;event&lt;/strong&gt;, and register a callback to be called when the event is posted.&lt;/p&gt;

&lt;p&gt;One or more sources can &lt;strong&gt;post&lt;/strong&gt; instances of the event. Each time the event is posted, all of the client's callbacks get triggered. Events carry no information, other than the fact that they "happened". This is what makes this different from a message queue.&lt;/p&gt;

&lt;p&gt;For the sake of simplicity, and because it suited my use case better, the code only guarantees that callbacks will get called at least once after each posted event, and not once for each posted event.&lt;/p&gt;

&lt;p&gt;This means that if an event is posted multiple times before the event subscribers were notified of the first one, then events after the first will not be counted as a separate instance, and subscribers will be called only once.&lt;/p&gt;

&lt;p&gt;This makes &lt;code&gt;EventQueue&lt;/code&gt; kind of a misnomer, because it's not really a queue if you can "miss" events. However, from a design point of view, since events carry no information with them, then nothing is lost because of this design choice unless you want to count events (which is perfectly valid use case, just not mine).&lt;/p&gt;

&lt;p&gt;The implementation of &lt;code&gt;EventQueue&lt;/code&gt; has a &lt;code&gt;postEvent()&lt;/code&gt; method for event sources, and a &lt;code&gt;subscribeToEvent()&lt;/code&gt; method to allow clients to register a callback.&lt;/p&gt;

&lt;p&gt;This is as simple as asynchronous message passing can get. And yet, there's still stuff to unpack here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unpacking the stuff &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Event sources should be decoupled from subscribers &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;It would be trivial to write &lt;code&gt;postEvent()&lt;/code&gt; so that it loops through a vector of client callbacks calling each, but that would mean that the called is &lt;em&gt;blocked&lt;/em&gt; by the clients while the callbacks execute. &lt;/p&gt;

&lt;p&gt;That couples the execution flow on the event source to the event receivers, and in a multi threaded where modules on each end should be protecting their internal states with mutexes, this kind of coupling is likely to end up causing a &lt;a href="https://en.wikipedia.org/wiki/Deadlock" rel="noopener noreferrer"&gt;deadlock&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We'll decouple the event source by making &lt;code&gt;postEvent()&lt;/code&gt; non-blocking. Posting the event only &lt;em&gt;triggers&lt;/em&gt; the event notification, but control will be returned to the sender right-away, potentially even before any of the subscribers have been notified.&lt;/p&gt;

&lt;p&gt;Notice that this goes beyond just making &lt;code&gt;postEvent()&lt;/code&gt; not to wait for the event to be delivered before returning. It's easy to forget that if &lt;code&gt;postEvent()&lt;/code&gt; needs to take ownership of the same &lt;code&gt;EventQueue&lt;/code&gt; mutex that gets taken by the parts of the code that deliver events to clients, then it might block trying to get access to that mutex while a previous event is getting delivered to event subscribers. In most applications this is almost as bad as blocking while the event gets delivered, and just as likely to cause a deadlock.&lt;/p&gt;

&lt;p&gt;No mutex in &lt;code&gt;postEvent()&lt;/code&gt;, got it. However, this method still needs to access the &lt;code&gt;EventQueue&lt;/code&gt; object internal state in a thread-safe manner, because callbacks will be executing from a separate thread.&lt;/p&gt;

&lt;p&gt;To do this, the code uses a &lt;a href="https://en.wikipedia.org/wiki/Monitor_(synchronization)#Condition_variables_2" rel="noopener noreferrer"&gt;condition variable&lt;/a&gt; to trigger the processing of the event in an event forwarder thread:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void EventQueue::postEvent() {
  pending_event_ = true;
  cv_.notify_all();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;postEvent()&lt;/code&gt; never locks. It just sets a boolean flag, notifies some code with a condition variable, and moves on. There's no way to block it.&lt;/p&gt;

&lt;p&gt;I'll go back to that flag later.&lt;/p&gt;

&lt;h4&gt;
  
  
  Subscribers can be decoupled from each other, but I won't &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Subscribers could be decoupled from each other too, but that's not my use case. I will just call each subscriber callback in the list sequentially, one after the other, every time an  event gets posted.&lt;/p&gt;

&lt;p&gt;This is unlikely to cause a deadlock by itself, since callbacks will usually take ownership of their target's mutex on entry and release it on exit. &lt;/p&gt;

&lt;p&gt;This can, however, cause additional processing delays if one the callbacks calls something blocking or takes a long time to execute, but we're in a &lt;a href="https://en.wikipedia.org/wiki/Cooperative_multitasking" rel="noopener noreferrer"&gt;cooperative-scheduling&lt;/a&gt; scenario here, and we are all good gentlemen.&lt;/p&gt;

&lt;p&gt;An alternative design would be to hold separate notification threads for each of the subscriber's callbacks, but in most cases the extra effort is not worth the cost if your subscriber's callbacks are guaranteed to be quick and non-blocking (and all callbacks should always be). &lt;/p&gt;

&lt;p&gt;If your callbacks can be considered unsafe or prone to being manipulated by an adversary then you would do well to play it safe and walk this extra mile out of precaution. This is seldom the case in monolitic software, however.&lt;/p&gt;

&lt;h4&gt;
  
  
  Identify the execution threads at play &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;When writing concurrent code you should always be aware of what  threads are at play around a given piece of code.&lt;/p&gt;

&lt;p&gt;There will be threads external to your object, such as the ones executing &lt;code&gt;postEvent()&lt;/code&gt;. These are potentially rooted in other objects in your system, and they are only temporarily executing code that belongs to your object (in this case &lt;code&gt;EventQueue&lt;/code&gt;). &lt;/p&gt;

&lt;p&gt;These external threads may be holding locks on mutexes elsewhere on the system (typically, the one protecting the object that created them), and may need to acquire additional  mutexes in order to gain access to protected assets within an object the need to interact with.&lt;/p&gt;

&lt;p&gt;Take for instance the case of a piece of code creating a copy of a pre-existing &lt;a href="https://en.cppreference.com/w/cpp/memory/shared_ptr" rel="noopener noreferrer"&gt;shared pointer&lt;/a&gt;. The execution thread (the one making the copy) is external to the shared pointer, and that thread needs to acquire the mutex within the shared pointer in order to update the pointer's reference count. It does so while executing the copy-assignment operator or the copy-constructor of the smart pointer class.&lt;/p&gt;

&lt;p&gt;External threads are enough for a large number of types of objects whose internal state is only updated by interactions through their public interface. The &lt;code&gt;std::shard_ptr&lt;/code&gt; class is an example of that kind of object.&lt;/p&gt;

&lt;p&gt;Other objects, however, also need to be able to &lt;em&gt;self-propel&lt;/em&gt; their own state forward, even in complete absence of external stimuli. These objects will usually have one or more internal threads -threads created by the object itself- that are there tasked with driving the evolution of the internal state of the objects through interactions with the exterior.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;EventQueue&lt;/code&gt; class is of this last kind: In order to decouple the event source from the subscribers the object needs to take upon itself the task of delivering the posted events to the subscribers. Executing the subscriber callbacks requires a thread, and therefore &lt;code&gt;EventQueue&lt;/code&gt; creates an internal thread to do this, one that we'll call &lt;code&gt;forwarderThread&lt;/code&gt; (since that's the name of the function that hosts its code).&lt;/p&gt;

&lt;p&gt;This thread spends most of its time asleep. Each time an event is posted through &lt;code&gt;postEvent()&lt;/code&gt; this internal thread gets woken up to take care of the delivery. The code of the thread looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void EventQueue::forwarderThread() {
  std::unique_lock&amp;lt;std::mutex&amp;gt; lock{mutex_};
  while (!halting_) {
    cv_.wait_for(
      lock,
      max_sleep_interval_,
      [this]() { return pending_event_ || halting_; }
    );
    if (pending_event_) {
      pending_event_ = false;
      triggerSubscribers();
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll talk a lot about this function, but let's go one step at a time. For now lets just say that this code spends most its time sleeping on the condition variable &lt;code&gt;cv_&lt;/code&gt; until something wakes it up using &lt;code&gt;cv_.notify_all()&lt;/code&gt;, such as for instance, a call to &lt;code&gt;postEvent()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;It's also important to understand that the thread owns the mutex while while it's awake, but the condition variable releases it while it sleeps in &lt;code&gt;wait_for()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Whenever a &lt;code&gt;cv_.notify_all()&lt;/code&gt; call wakes up the forwarder thread, it re-acquires the mutex and springs to action, delivering an event to clients by calling their registered callbacks. After doing that, it goes back to sleep.&lt;/p&gt;

&lt;p&gt;Only the destructor can terminate this loop, by setting the &lt;code&gt;halting_&lt;/code&gt; flag to indicate that the thread needs to exit. This is important, because threads need to be &lt;em&gt;joined&lt;/em&gt; before destroying them. &lt;code&gt;thread::join()&lt;/code&gt; is a blocking call that waits until a thread terminates execution; if a thread is destroyed without having been joined, it will kill your system as steadfastly as a segmentation fault, and with about the same amount of pomp. The &lt;code&gt;halting_&lt;/code&gt; flag tells the thread that it needs to exit, so that it can be joined. It's like whistling to bring back your dog. &lt;/p&gt;

&lt;h4&gt;
  
  
  Before we move on: callbacks are evil &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;That's true. They are.&lt;/p&gt;

&lt;p&gt;A callback is someone else's code that gets executed on your thread. Or it's your code, that executes on someone else's thread. Either way, you're not fully in the driving seat; you're literally sharing the seat.&lt;/p&gt;

&lt;p&gt;Also, at its very best, a callback is just a raw pointer on one module pointing to another, and this raises lots of questions regarding the relative life scope of each object with repect to the other. Think of this scenario.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;ObjectA&lt;/code&gt; owns &lt;code&gt;ObjectX&lt;/code&gt; and &lt;code&gt;ObjectY&lt;/code&gt;. During construction it set a callback in &lt;code&gt;ObjectY&lt;/code&gt; to trigger a method in &lt;code&gt;ObjectX&lt;/code&gt; when a message &lt;code&gt;M&lt;/code&gt; is received. &lt;code&gt;ObjectA&lt;/code&gt; is destroyed, and during the termination sequence it first destroys &lt;code&gt;ObjectX&lt;/code&gt;, then &lt;code&gt;ObjectY&lt;/code&gt;. After the demise of &lt;code&gt;ObjectX&lt;/code&gt;, but before the end of &lt;code&gt;ObjectY&lt;/code&gt;, a message &lt;code&gt;M&lt;/code&gt; arrives, and the callback gets triggered an calls the method on &lt;code&gt;ObjectX&lt;/code&gt;, all of whose data members have already been destroyed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hear you say:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;"That's bad design, you have horizontally rigged your system and your architecture does not enforce a functional dependency between X and Y"&lt;/em&gt;. It's true, I say, but the callback making the system crash did not make this any better. Also, even if the callback was hosted in &lt;code&gt;ObjectA&lt;/code&gt; instead of &lt;code&gt;ObjectX&lt;/code&gt;, this sequence of events would still cause a crash if the callback code used &lt;code&gt;ObjectX&lt;/code&gt; during its execution.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;"That's a corner case, the system is winding down anyway so no harm is done"&lt;/em&gt;. A corner case, true; harmless, false. You're potentially wrecking the destruction process. I've seen code like this try to acquire an already destroyed mutex, which is undefined behavior, and then block forever causing the software to linger until I had to SIGKILL it. If you're "lucky", your code will crash right away, but even in that case you may lose information that was supposed to be orderly stored by your termination sequence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This only gets worse if you think of a case in which you have  subscribers that come and go during the life scope of the callback-calling class. If the subscriber objects are temporary you need to make sure you unsubscribe from the callback before you destruct each of them. But there's no way to enforce this, because a callback is just a raw pointer, so it's error prone and it's easy to cause a software crash during execution.&lt;/p&gt;

&lt;p&gt;Callbacks are a necessary evil, though. But we can make them a bit more RAII. More on that later.&lt;/p&gt;

&lt;h4&gt;
  
  
  volatile vs atomic &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;You can injure yourself if you don't know this. If more than one thread is at play, there are some guarantees that are lost. &lt;/p&gt;

&lt;p&gt;The short version is, if you have two asynchronous execution threads (for instance two Linux process threads, but also a task's code and an interrupt service routine on a humble embedded RTOS), and you want them to signal each other through a variable such as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bool halting_{false};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then that's a recipe for failure. &lt;/p&gt;

&lt;p&gt;The reason for that is that the compiler does not know about that hidden Read-After-Write (RAW) dependency between the writes on one flow of execution and the reads on the second one, and it's very likely to either partially or totally optimize the connection through memory away.&lt;/p&gt;

&lt;p&gt;This can happen in a number of ways, but the most frequent one is because any compiler worth its salt will optimize your object code so that variables get cached within CPU registers for as long as possible to improve the speed of access, and no actual memory accesses will really be performed except for reading the initial value of the variable at the start of the use scope, and storing the final value at the very end of it. If your code is waiting for a variable value to change in a loop, caused by a source that the compiler does not know about, then your code will most likely wait forever.&lt;/p&gt;

&lt;p&gt;Notice if you risk it and decide to use a regular variable to synchronize independent execution flows, you may find that everything works "as expected", but the actual outcome will vary depending on the architecture of your code, your build configuration, the version of your compiler and what you had for breakfast last week. Your code may start failing a few minutes/days/months/years later when someone else makes an unrelated change to the code, updates the compiler version, or eats last-night's pizza for breakfast instead of butter on bread.&lt;/p&gt;

&lt;p&gt;So bare variables is a no-no. How about &lt;code&gt;volatile&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;volatile bool halting_{false};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a big no-no too, but it's a different kind of error. Lots of people think &lt;code&gt;volatile&lt;/code&gt; can be used for blissful inter-thread communication through a shared variable, and that's correct for other programming languages, and it even works for C++ code running on simpler single-threaded processors with simple memory hierarchies.&lt;/p&gt;

&lt;p&gt;It's not true in general, however, because &lt;code&gt;volatile&lt;/code&gt; is not meant to be used for that, and you should not use it that way because the outcome in that case would be undefined. Quoting directly &lt;a href="https://en.cppreference.com/w/cpp/language/cv" rel="noopener noreferrer"&gt;from cppreference.com on the topic&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every access (read or write operation, member function call, etc.) made through a glvalue expression of volatile-qualified type is treated as a visible side-effect for the purposes of optimization (that is, within a single thread of execution, volatile accesses cannot be optimized out or reordered with another visible side effect that is sequenced-before or sequenced-after the volatile access. &lt;strong&gt;This makes volatile objects suitable for communication with a signal handler, but not with another thread of execution&lt;/strong&gt;, see std::memory_order)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The prose here admittedly a bit obscure but the warning is clear. A much clearer discussion of the topic can be found in &lt;a href="https://www.oreilly.com/library/view/effective-modern-c/9781491908419/" rel="noopener noreferrer"&gt;Effective Modern C++&lt;/a&gt;, which I encourage you to read. In there, Meyers states that the guideline is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use &lt;code&gt;std::atomic&lt;/code&gt; for concurrency, &lt;code&gt;volatile&lt;/code&gt; for special memory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Special memory in the context of the quote means, for instance, a memory-mapped I/O register in a micro-controller.&lt;/p&gt;

&lt;p&gt;So, what is &lt;code&gt;std::atomic&lt;/code&gt;? It's a template that "decorates" a bare type in such a way as to ensure two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Updates to the variable of an atomic type are seen as atomic by readers. That means they will either see the value after or before the update, but never a mixture or partially updated state.&lt;/li&gt;
&lt;li&gt;The variable of an atomic type performs inter-thread memory synchronization on updates, making sure that after a thread writes on it, any other thread that reads the same variable will read the updated value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What's the downside of this? why not default to atomic behavior? As you are very likely expecting, because it's not cheap, an atomic access is always more expensive than an access to a regular type variable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There's no in-register caching of an atomic variable. That means no single-cycle read/write access even for atomics of simple integer-like types, such as &lt;code&gt;bool&lt;/code&gt;. You'll always go through the memory hierarchy to access them, and pay the price for it.&lt;/li&gt;
&lt;li&gt;Accesses to an atomic trigger a synchronization process between the caches of different cores in the CPU to ensure a consistent event-ordering for all threads on the system. This is a punch right on the performance on any modern multi-core, deeply-pipelined processor.&lt;/li&gt;
&lt;li&gt;For smaller datatypes (&lt;code&gt;bool&lt;/code&gt;, for instance), &lt;code&gt;std::atomic&amp;lt;T&amp;gt;&lt;/code&gt; may use an implementation that uses atomic load/store instructions available on most modern processors, but for larger or user-defined datatypes, &lt;code&gt;std::atomic&lt;/code&gt; will basically attach a mutex to the variable and acquire/release around each access. This can be even more expensive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, and here I'm setting the text in bold again: &lt;strong&gt;use std::atomic for unprotected variables you share between two threads, and use them sparingly&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;That's why in &lt;code&gt;EventQueue&lt;/code&gt; we use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;std::atomic_bool pending_event_{false};
std::atomic&amp;lt;bool&amp;gt; halting_{false};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to transfer information between the some external thread executing &lt;code&gt;postEvent()&lt;/code&gt; or the &lt;code&gt;EventQueue&lt;/code&gt; destructor, and the internal thread that drives event delivery to subscribers. We &lt;em&gt;need that information to get through&lt;/em&gt; across threads.&lt;/p&gt;

&lt;p&gt;As a final note on this section, &lt;em&gt;unprotected&lt;/em&gt; in the bold text means &lt;em&gt;out of the protection scope of a mutex&lt;/em&gt;. See next section.&lt;/p&gt;

&lt;h4&gt;
  
  
  What if I have lots of shared variables between threads? &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;This is frequently the case. Most often your object's internal state is made of a multitude of internal data members, and updates to that state can be triggered by code executed by  either internal or external threads.&lt;/p&gt;

&lt;p&gt;In that case you don't make everything &lt;code&gt;std::atomic&amp;lt;T&amp;gt;&lt;/code&gt;. That would only make access to your individual variables atomic, but the internal state of your class would still not be thread-safe, because different threads execution flows could still intermingle in undefined-and-most-likely-not-safe ways.&lt;/p&gt;

&lt;p&gt;Take for instance,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EventQueue::CallbackHandle EventQueue::subscribeToEvent(const CallbackFunction &amp;amp;callback) {
  std::lock_guard&amp;lt;std::mutex&amp;gt; lock{mutex_};
  auto handler = std::make_shared&amp;lt;CallbackFunction&amp;gt;(callback);
  subscribers_.emplace_back(std::weak_ptr&amp;lt;CallbackFunction&amp;gt;(handler));
  return handler;
}

void EventQueue::forwarderThread() {
  std::unique_lock&amp;lt;std::mutex&amp;gt; lock{mutex_};
  while (!halting_) {
    cv_.wait_for(lock, max_sleep_interval_, [this]() { return static_cast&amp;lt;bool&amp;gt;(pending_event_); });
    if (pending_event_) {
      pending_event_ = false;
      triggerSubscribers();
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These can execute concurrently; the first one is driven by an external thread, from some other object on the system makin g a call on &lt;code&gt;EventQueue&lt;/code&gt;, while the second one is our internal thread's main body.&lt;/p&gt;

&lt;p&gt;Both are somewhat complex functions, especially the second one. You might be tempted to say that the first one only makes an update to the &lt;code&gt;subscribers_&lt;/code&gt; vector, and that that operation can be made atomic with &lt;code&gt;std::atomic&amp;lt;T&amp;gt;&lt;/code&gt;, and that would be true, but as you'll see later, &lt;code&gt;triggerSubscribers()&lt;/code&gt; &lt;strong&gt;iterates&lt;/strong&gt; on that vector, and iterating is not an atomic operation, it is a sequence of operations.&lt;/p&gt;

&lt;p&gt;That's why we need to prevent these two methods from executing at the same time, and to do that we force them to only make changes to the state of the object (that is, to any member of the object) while holding a lock on a shared &lt;code&gt;std::mutex&lt;/code&gt; variable. &lt;/p&gt;

&lt;p&gt;The mutex can only be owned by a single thread thread at the same time. If we enforce that all changes to the state will only be done while owning the mutex, then that forces all updates to the state to be made in an ordered fashion, and each individual change can be thought of as atomic.&lt;/p&gt;

&lt;p&gt;Notice that enforcing this is up to the developer. Nothing prevents me from updating the state without holding the mutex, but that's usually unsafe unless you have some other way to make sure of making changes to the object state in an atomic fashion. &lt;code&gt;postEvent()&lt;/code&gt; does not use a mutex, but it only makes a very bounded type of change by setting an &lt;code&gt;std::atomic_bool&lt;/code&gt; flag, so we're safe (to the extent anything in concurrency is safe).&lt;/p&gt;

&lt;p&gt;There are many ways to lock a mutex, but two are the most common and both are on display in the code above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;std::lock_guard&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::unique_lock&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;std::lock_guard&lt;/code&gt; is the simplest one. In fact, it only has two methods: the constructor and the destructor. An instance of this class locks the mutex on when it gets constructored, and releases it when it's destroyed. This is effectively a use of the RAII idiom, where the resource being owned is the lock on the mutex.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::unique_lock&lt;/code&gt;, is behaves in mostly the same way, but also has a few more features. The defining one is that it provides &lt;code&gt;lock()&lt;/code&gt; and &lt;code&gt;unlock()&lt;/code&gt; methods to temporarily release the lock manually. &lt;code&gt;std::condition_variable::wait_for()&lt;/code&gt; uses these to unlock the  mutex while it waits for the condition variable notification, so that's the reason &lt;code&gt;std::unique_lock&lt;/code&gt; needs to be used there.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::unique_lock&lt;/code&gt; provides a superset of the features of &lt;code&gt;std::lock_guard&lt;/code&gt;, but you should always prefer the later unless you really need the features in the first one.&lt;/p&gt;

&lt;p&gt;The creation of the lock (either &lt;code&gt;lock_guard&lt;/code&gt; or &lt;code&gt;unique_lock&lt;/code&gt;) should be the first sentence in the scope you want to protect, usually a public method of your object; anything before that would not be protected by the mutex. Everything after that all the way to the end of the scope will be.&lt;/p&gt;

&lt;p&gt;Ok, locking a mutex gives us mutual exclusion, and therefore atomicity. What about inter-thread synchronization to guarantee side-effect ordering consistency on memory accesses and stuff? &lt;code&gt;std::atomic&amp;lt;T&amp;gt;&lt;/code&gt; was big on this too.&lt;/p&gt;

&lt;p&gt;Well, each time you lock/unlock a mutex memory access synchronization is guaranteed so that any variable you update within the scope of the lock will be visible with the updated value to any other thread that wants to read it later.&lt;/p&gt;

&lt;p&gt;So, if you protect all accesses to a variable within the scope of locked mutex, the variable can be just a regular variable, because the mutex will ensure synchronization on lock/unlock. That's the case for &lt;code&gt;subscribers_&lt;/code&gt;, but not for &lt;code&gt;pending_event_&lt;/code&gt;, which is also updated from &lt;code&gt;postEvent()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As as I said earlier, &lt;code&gt;postEvent()&lt;/code&gt; does not lock the mutex to avoid having to potentially wait for the mutex to be released while the callbacks are getting executed. To make sure the updates to &lt;code&gt;pending_event_&lt;/code&gt; are seen by the forwarder thread we need to make that variable and &lt;code&gt;std::atomic_bool&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This design choice will impact something else, one that can be the source of a very subtle bug if you're not careful.&lt;/p&gt;

&lt;h4&gt;
  
  
  Callbacks, yes, but RAII-ish &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Callbacks are a necessary evil, I said. I need callbacks on this code.&lt;/p&gt;

&lt;p&gt;However, instead of going for raw callbacks that would have to be both registered and unregistered manually by event subscribers, this code tries to at least ensure that callbacks will get automatically unregistered at the very moment when a handler returned by the &lt;code&gt;subscribeToEvent()&lt;/code&gt; method goes out of scope.&lt;/p&gt;

&lt;p&gt;The idea is that a subscriber will register a callback to  receive notifications from the event by doing something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto handler = event_queue.subscribeToEvent(
  []() { doSomething(); }
); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The callback (in this case a lambda) will remain registered to the event represented by &lt;code&gt;event_queue&lt;/code&gt; instance all the way until the point where &lt;code&gt;handler&lt;/code&gt; goes auto of scope or is intentionally destroyed.&lt;/p&gt;

&lt;p&gt;Once the handler goes out of scope, the &lt;code&gt;event_queue&lt;/code&gt; instance will automatically remove the callback from the &lt;code&gt;subscribers_&lt;/code&gt; list with no need of any further interaction from the subscriber.&lt;/p&gt;

&lt;p&gt;To implement this the code uses &lt;code&gt;std::weak_ptr&lt;/code&gt;, which is a weak version of &lt;code&gt;shared_ptr&lt;/code&gt; that does not hold ownership over the pointed-to-data, but that allows us to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check if an associated &lt;code&gt;shared_ptr&lt;/code&gt; still exists.&lt;/li&gt;
&lt;li&gt;Get a copy of the associated &lt;code&gt;shared_ptr&lt;/code&gt; if at least one other copy still exists.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, keeping in mind that &lt;code&gt;EventQueue&lt;/code&gt; defines following aliases in class scope,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  using CallbackFunction = std::function&amp;lt;void()&amp;gt;;
  using CallbackHandle = std::shared_ptr&amp;lt;CallbackFunction&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the implementation of &lt;code&gt;subscribeToEvent()&lt;/code&gt; becomes is very simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  auto handler = std::make_shared&amp;lt;CallbackFunction&amp;gt;(callback);
  subscribers_.emplace_back(std::weak_ptr&amp;lt;CallbackFunction&amp;gt;(handler));
  return handler;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All this does is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It creates the original &lt;code&gt;shared_ptr&amp;lt;CallbackFunction&amp;gt;&lt;/code&gt;. This shared pointer is the &lt;code&gt;CallbackHandle&lt;/code&gt; that &lt;code&gt;subscribeToEvent()&lt;/code&gt; will return to the caller.&lt;/li&gt;
&lt;li&gt;It creates a &lt;code&gt;weak_ptr&amp;lt;CallbackFunction&amp;gt;&lt;/code&gt; associated to the &lt;code&gt;shared_ptr&lt;/code&gt; created above, and stores that weak pointer in the internal list of subscribers. This weak pointer will be used by &lt;code&gt;EventQueue&lt;/code&gt; to track which subscribers have been destroyed before calling on their callbacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;triggerSubscribers()&lt;/code&gt; method, which gets called by &lt;code&gt;forwarderThread()&lt;/code&gt; and is therefore within the mutex protection scope of the second's mutex lock, basically only does two things: it removes any &lt;code&gt;weak_ptr&lt;/code&gt; whose associated &lt;code&gt;shared_ptr&lt;/code&gt; has gone out of scope, and then it iterates on the remaining subscribers calling the callback on each.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void EventQueue::triggerSubscribers() {
  subscribers_.remove_if([](const EventQueue::WeakCallbackHandler &amp;amp;handler) { return handler.expired(); });
  ...
  std::for_each(subscribers_.begin(), subscribers_.end(), caller);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the &lt;code&gt;caller()&lt;/code&gt; lambda needs to check again whether the weak_ptr is still valid, because the &lt;code&gt;shared_ptr&lt;/code&gt; might have been destroyed before the call to &lt;code&gt;subscribers_.remove_if()&lt;/code&gt; but before &lt;code&gt;std::for_each()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto caller = [](const EventQueue::WeakCallbackHandler &amp;amp;weak_handler) {
  auto handler = weak_handler.lock();
  if (handler) {
    (*handler)();
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that while this is an improvement manually registering/unregistering callbacks, there's still one issue with this code. We'll go back to it in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  The devil hides is in the details &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;There's more than one devil hidden here. &lt;/p&gt;

&lt;h3&gt;
  
  
  The "lost event" pitfall &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The first hidden devil, which is easy to fall for, is also relatively straightforward to detect and understand.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;forwarderThread()&lt;/code&gt;, if we change&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (pending_event_) {
  pending_event_ = false;
  triggerSubscribers();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;for&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (pending_event_) {
  triggerSubscribers();
  pending_event_ = false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then we are introducing a bug that could very easily be missed during testing unless we are looking for it.&lt;/p&gt;

&lt;p&gt;Setting the flag to &lt;code&gt;false&lt;/code&gt; &lt;strong&gt;after&lt;/strong&gt; running the callbacks means that if the event is posted again after checking the value at &lt;code&gt;if (pending_event_)&lt;/code&gt; but before &lt;code&gt;pending_event_ = false;&lt;/code&gt;, the event might be missed by some of the subscribers.&lt;/p&gt;

&lt;p&gt;Instead, setting it &lt;code&gt;false&lt;/code&gt; &lt;strong&gt;before&lt;/strong&gt; running the callbacks means that regardless of where a second event falls, the guarantee that all callbacks will be called at least once after  each posted event still holds for all subscribers.&lt;/p&gt;

&lt;p&gt;That one was easy.&lt;/p&gt;

&lt;h3&gt;
  
  
  The "deaf condition variable" pitfall (part I) &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;You may have noticed that the &lt;code&gt;wait_for()&lt;/code&gt; call in &lt;code&gt;forwarderThread()&lt;/code&gt; has a timeout parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cv_.wait_for(
    lock,
    max_sleep_interval_,
    [this]() { return pending_event_ || halting_; }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's perfectly reasonable to question this: Why would a timeout be needed here? This is code that should only wake up whenever there's an event to deliver, there's not really a reason to wake it up just because a timeout has expired.&lt;/p&gt;

&lt;p&gt;Even more so, since the timeout causes the condition variable to be able to wake up for reasons other than receiving an event, it needs to check if an event was posted when it wakes up, and if not it'll just go back to sleep!&lt;/p&gt;

&lt;p&gt;The first part is almost true, and we'll go back to that later. The the second one less so, because the destructor may also wake the condition variable, and we don't want to trigger the callbacks spuriously because &lt;code&gt;EventQueue&lt;/code&gt; is getting destroyed, so we need to check if there's an event to deliver to clients regardless of the timeout.&lt;/p&gt;

&lt;p&gt;Let's take a look at how destruction takes place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EventQueue::~EventQueue() {
  halting_ = true;
  cv_.notify_all();
  forwarder_thread_.join();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a typical way to terminate an internal thread in objects that have one.&lt;/p&gt;

&lt;p&gt;Notice that we don't lock the mutex in this method. We can't. &lt;/p&gt;

&lt;p&gt;If we did &lt;code&gt;cv_.wait_for()&lt;/code&gt; in &lt;code&gt;forwarderThread()&lt;/code&gt; would be unable to wake up because it would not be possible for it to lock the mutex, and the destructor would block in &lt;code&gt;forwarder_thread_.join()&lt;/code&gt; waiting for the thread to terminate execution. Since both threads are waiting for the other to move forward to move themselves, no progress can be made. This is a classic deadlock scenario.&lt;/p&gt;

&lt;p&gt;To avoid this the destructor does not try to lock the mutex and instead it signals the intention to kill the thread through an &lt;code&gt;std::atomic_bool&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Notice that, because the destructor code as a whole is not atomic, the order of the instructions matters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;halting_ = true;
cv_.notify_all();
forwarder_thread_.join();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the order of these lines is changed the forwarder thread might not wake up, or might wake up and miss the halting flag and go back to sleep&lt;/p&gt;

&lt;p&gt;We're good then. We won't miss the flag because we are setting stuff in the right order.&lt;/p&gt;

&lt;p&gt;Not so.&lt;/p&gt;

&lt;h3&gt;
  
  
  The "deaf condition variable" pitfall (part II, the revenge) &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;There's more to say about the condition variable. Let's talk about the lambda in the third argument to &lt;code&gt;wait_for&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's start by stating that, in theory, this should be enough for the code to work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while (!halting_) {
  cv_.wait(lock);
  if (pending_event_) {
    ...
  }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both the destructor notifications and new event that are posted  would be able to wake this loop and process the input acording to the source. However this has at least two issues. &lt;/p&gt;

&lt;p&gt;The first one is that a condition variable will not be aware of any &lt;code&gt;notify_all()&lt;/code&gt;/&lt;code&gt;notify_one()&lt;/code&gt; that is called while it's awake.&lt;/p&gt;

&lt;p&gt;So, if the destructor gets called in the unlucky interval in which &lt;code&gt;forwarderThread()&lt;/code&gt; goes from evaluating &lt;code&gt;while (!halting_)&lt;/code&gt; to setting the thread to sleep in &lt;code&gt;cv_.wait(lock);&lt;/code&gt;, then you're dead. Unresponsive. The destructor will block waiting for the thread to terminate, but the thread will be sleeping and won't wake up because it missed the condition variable notification and the flag.&lt;/p&gt;

&lt;p&gt;Notice that a very similar scenario would miss (or at least delay) a newly posted event if the notification arrives while &lt;code&gt;forwarderThread()&lt;/code&gt; is still awake delivering a previous event. &lt;/p&gt;

&lt;p&gt;In both cases the system is not really totally blocked, because the lucky arrival of a new &lt;code&gt;postEvent()&lt;/code&gt; call would bring the loop back to the land of the living by waking up the &lt;code&gt;forwarderThread()&lt;/code&gt;, but you should not count on this.&lt;/p&gt;

&lt;p&gt;Also, it also turns out that condition variables have the annoying habit of sometimes waking up just because the feel like it. If you don't want to take my word on it, you can read &lt;a href="https://en.cppreference.com/w/cpp/thread/condition_variable/wait" rel="noopener noreferrer"&gt;the very small print in one cppreference.com&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It may also be unblocked spuriously.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before you wonder, &lt;a href="https://en.wikipedia.org/wiki/Spurious_wakeup" rel="noopener noreferrer"&gt;it's not a bug, it's feature&lt;/a&gt;. It's just too hard to create condition variable implementations that don't do that, so we need to live and code with this in mind.&lt;/p&gt;

&lt;p&gt;To avoid spurious awakenings, a second form of the &lt;code&gt;wait_for()&lt;/code&gt; call provides a &lt;em&gt;predicate&lt;/em&gt; function parameter. This is usually just a lambda that should return &lt;code&gt;false&lt;/code&gt; if the condition for awakening is not satisfied. When a predicate is provided, the condition variable will check the predicate while waking up and if the value is false then it will assume that it woke up spuriously and go back to sleep.&lt;/p&gt;

&lt;p&gt;Agreed, the predicate sounds like a good idea. The code would look like this now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while (!halting_) {
  cv_.wait_for(
     lock,
     [this]() { return pending_event_ || halting_; }
  );
  if (pending_event_) {
    ...
  }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the predicate not only solves the issue of spurious awakenings. It also fixes missed notifications! if &lt;code&gt;postEvent()&lt;/code&gt; got called while the code was running the callbacks, then when it loops back to the &lt;code&gt;wait_for()&lt;/code&gt; call the predicate will be true, so the condition variable will not even go into sleep mode and a new processing round will start.&lt;/p&gt;

&lt;p&gt;Similarly, it also fixes the unfortunate case where the destructor gets called in that narrow stretch between the while and the &lt;code&gt;wait_for&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's it, right? we're done! the timeout was useless.&lt;/p&gt;

&lt;p&gt;Not so. Concurrency is such a fragile thing.&lt;/p&gt;

&lt;p&gt;It may not look like it from our abstraction level, but&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[this]() { return pending_event_ || halting_; }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;actually takes some time to get executed. A small one, but still. There's non-zero chance that the two lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; halting_ = true;
 cv_.notify_all();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will get executed after &lt;code&gt;halting_&lt;/code&gt; was evaluated by the lambda, but before &lt;code&gt;cv_.wait_for()&lt;/code&gt; got to sleep. &lt;/p&gt;

&lt;p&gt;The end result is the same as before: the destructor blocks, waiting for the thread to terminate, but the forwarder thread misses the notification and goes to sleep with no-one to wake it up because the destructor won't try twice.&lt;/p&gt;

&lt;p&gt;Worse, a similarly ill-timed call to &lt;code&gt;postEvent()&lt;/code&gt; can suffer the same fate.&lt;/p&gt;

&lt;p&gt;So, after adding the predicate our worst possible case is still the same as before: blocking for an indeterminate amount of time. We just narrowed the time window during which that may happen.&lt;/p&gt;

&lt;p&gt;Feeling depressed? I know the feeling. But hey! what are the chances, right? You need to be really unlucky to hit this issue!&lt;/p&gt;

&lt;p&gt;Not so unlucky, it turns out. The repository provides a few units tests along with the &lt;code&gt;EventQueue&lt;/code&gt; code. This is the first one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TEST_F(QueueEventTest, ConstructionTest) {
  auto uut = std::make_unique&amp;lt;EventQueue&amp;gt;();
  uut = nullptr;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This unit test only creates an EventQueue and then destroys it right away. It's a perfectly valid test that checks the least amount of functionality that can be tested: construction and destruction don't blow up.&lt;/p&gt;

&lt;p&gt;Turns out that this test hits the sweet spot for the above described missed-notification-failure frequently enough that if that test is run on a &lt;a href="https://en.wikipedia.org/wiki/Continuous_integration" rel="noopener noreferrer"&gt;CI&lt;/a&gt; you coworkers would be upset by the random failures of your test very soon after it gets added.&lt;/p&gt;

&lt;p&gt;There are two solutions to this:&lt;/p&gt;

&lt;h5&gt;
  
  
  Lock the mutex around any code that may wake the condition variable
&lt;/h5&gt;

&lt;p&gt;If we set a lock to own the mutex around the code that sets the variables and signals the condition variable to wake up, this would fix the problem, because since the forwarder thread owns the mutex while it's awake, then mutual exclusion would ensure that flag updates and wake up notifications only get sent while it is asleep!&lt;/p&gt;

&lt;p&gt;Great!&lt;/p&gt;

&lt;p&gt;No! As I said before, we can't lock the mutex in the destructor. Not for the whole length of it, at least. We could, potentially, do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EventQueue::~EventQueue() {
  {
    std::lock_guard&amp;lt;std::mutex&amp;gt; lock{mutex_};
    halting_ = true;
  }
  cv_.notify_all();
  forwarder_thread_.join();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's not pretty, but it would get the job done, and in this case &lt;code&gt;halting_&lt;/code&gt; could be just a regular &lt;code&gt;bool&lt;/code&gt; instead of an &lt;code&gt;std::atomic_bool&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;However, the problem with not allowing anyone to do anything unless they own the mutex is that, well, no-one can do anything unless they own the mutex. That includes &lt;code&gt;postEvent()&lt;/code&gt; which would have to wait until the mutex is released before being able to post a new event. This might cause the &lt;code&gt;postEvent()&lt;/code&gt; caller to block while a callback is waiting for a different mutex, opening the opportunity for an indirect deadlock.&lt;/p&gt;

&lt;p&gt;Alternative designs can avoid this, at the cost of some additional complexity, but more complexity also means more potential for other subtle bugs like this creeping in the system.&lt;/p&gt;

&lt;h5&gt;
  
  
  The cheap solution
&lt;/h5&gt;

&lt;p&gt;The chances of this failure scenario happening are very low. If we keep damage low too, total risk is low too. If risk is low, the cost/benefit is on the side of a cheap-but-safe fix, rather than a complex fix that may open the door to equally complex bugs.&lt;/p&gt;

&lt;p&gt;Turns out a very the cheap solution is to use a timeout.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cv_.wait_for(
    lock,
    max_sleep_interval_,
    [this]() { return pending_event_ || halting_; }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The timeout puts an upper bound to the time the code can be blocked if any of the unfortunate scenarios I described above play out: if something like that happens, you system won't block for an indeterminate amount of time; in the worst case it will only block for the duration of the timeout.&lt;/p&gt;

&lt;p&gt;The side effect is that the loop in &lt;code&gt;forwarderThread()&lt;/code&gt; is going to wake up periodically even when nothing is happening, so most of the time the thread will wake up only to go back to sleep again.&lt;/p&gt;

&lt;p&gt;However, if your timeout is reasonable enough, the cost of periodically waking because of the timeout is negligible, and reasonable to keep bugs at bay.&lt;/p&gt;

&lt;p&gt;I won't go that far as to claim that this is the best solution to this. It's not a compromise I'm particularly happy to make, but it's one that usually gets the work done. I'll be happy to hear ideas for alternative solutions for this!&lt;/p&gt;

&lt;h4&gt;
  
  
  One more, for the road &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Finally, let's go back to the &lt;code&gt;caller()&lt;/code&gt; lambda in  &lt;code&gt;triggerSubscribers()&lt;/code&gt;. As I said before, this only partially solves some of the issues I enumerated about callbacks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto caller = [](const EventQueue::WeakCallbackHandler &amp;amp;weak_handler) {
  auto handler = weak_handler.lock();
  if (handler) {
    (*handler)();
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's a kind of worrying problem here, and you  may have already noticed it.&lt;/p&gt;

&lt;p&gt;While &lt;code&gt;weak_handler.lock()&lt;/code&gt; will atomically return a shared pointer to the callback, or a &lt;code&gt;nullptr&lt;/code&gt; if no more copies of the callback exist, there's no way to guarantee, with this implementation, that the handler will not be destroyed after&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto handler = weak_handler.lock(); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but before the handler callback gets called in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(*handler)();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Crap! We might be calling a callback right after the client decided it did not want any more calls! This is a completely plausible scenario, in which, for a short moment, the only remaining copy of the callback handler is the one in the &lt;code&gt;caller()&lt;/code&gt; lambda, which is for sure not the intention of the code here.&lt;/p&gt;

&lt;p&gt;I won't go as far as fixing this here. The article is already long is it stands now, so I'll let the reader figure this one out. &lt;/p&gt;

&lt;p&gt;A general idea would be that we need to make getting a copy of the shared pointer, testing and running the handler an &lt;em&gt;atomic block&lt;/em&gt; with respect to callback handler destruction. That is, either all of this gets executed &lt;em&gt;before&lt;/em&gt; destruction of the handle ends on the client side, or &lt;em&gt;none&lt;/em&gt; of it does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overcoming concurrency's hardships &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The code for &lt;code&gt;EventQueue&lt;/code&gt; is almost as simple as any multi-threaded object can be in C++1x, and yet at no point were we short of potential problems.&lt;/p&gt;

&lt;p&gt;Concurrency is hard. Even well-weathered professionals with years coding multi threaded software find themselves amused by new ways in which a system can throw itself into a pit, by managing to deadlock through  a loop formed by three different mutexes in different places of the system when the input message timing is just unlucky enough.&lt;/p&gt;

&lt;p&gt;Fortunately there's a solution for those endless hours of debugging multi threaded code.&lt;/p&gt;

&lt;h4&gt;
  
  
  The solution: write bug-free concurrent code &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;When creating concurrent code, testing will just not cut it, so you need to write flawless code.&lt;/p&gt;

&lt;p&gt;That's it. Solved. You´re welcome.&lt;/p&gt;

&lt;h4&gt;
  
  
  That's wishful thinking. What else have you got? &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Ok, the next best thing then. The next best thing right after writing flawless code is writing safe code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Be conservative. Assume the worst possible timing for anything that might happen, worry that you algorithm can be unscheduled and replaced by a competing thread at any moment, keep in mind that the system is full of latencies, and that things will never execute in any particular order unless you force it through explicit synchronization.&lt;/li&gt;
&lt;li&gt;Keep it simple. In concurrency complexity arises naturally from the huge number of ways in which different execution flows can be shuffled like a pack of cards. The more complex your code is, the larger the number of execution outcomes that can result from those combinations. The fewer possible outcomes, the easier it is to plan for all of them.&lt;/li&gt;
&lt;li&gt;Correctness over performance. Concurrency is very hard to debug. If your code is fast but only a bit buggy, you'll spend more time debugging than your code will ever save compared to a simpler but less error-prone solution.&lt;/li&gt;
&lt;li&gt;When given the choice between competing design solutions use the most conservative one that still makes sense for your system.&lt;/li&gt;
&lt;li&gt;Your code will still fail, so when possible fail gracefully.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is not a procedure. It's a mindset. And it's kind-of-obvious one, but most people don't code like this citing some pretext that usually alludes to &lt;em&gt;efficiency&lt;/em&gt;, &lt;em&gt;performance&lt;/em&gt;, &lt;em&gt;execution time&lt;/em&gt; or some other imaginary quality of their software that they have usually only measured in their minds.&lt;/p&gt;

&lt;p&gt;This mindset is especially important for concurrent software, because most bugs, if present, will get through testing and into production code unseen and unheard, like ninjas.&lt;/p&gt;

&lt;h2&gt;
  
  
  That's it, the end &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;That was much longer than I though when I started. Thanks for reading this far. Please let me know if you liked it!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
