<?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: Rondale Sidbury</title>
    <description>The latest articles on DEV Community by Rondale Sidbury (@milez-aac6b7eb9).</description>
    <link>https://dev.to/milez-aac6b7eb9</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%2F3670401%2Fabadcf28-e55b-4b49-a00b-34741b657ee9.jpg</url>
      <title>DEV Community: Rondale Sidbury</title>
      <link>https://dev.to/milez-aac6b7eb9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/milez-aac6b7eb9"/>
    <language>en</language>
    <item>
      <title>U CANT FALL OUT THE BED IF U SLEEP ON THE FLOOR.... "


 "Martin Lawrence"

:increment() {\n // Locks the mutex. When the function ends, the lock is automatically released.\n std::lock_guard&lt;std::mutex&gt; l</title>
      <dc:creator>Rondale Sidbury</dc:creator>
      <pubDate>Sat, 04 Apr 2026 06:06:18 +0000</pubDate>
      <link>https://dev.to/milez-aac6b7eb9/u-cant-fall-out-the-bed-if-u-sleep-on-the-floor-4fh5</link>
      <guid>https://dev.to/milez-aac6b7eb9/u-cant-fall-out-the-bed-if-u-sleep-on-the-floor-4fh5</guid>
      <description></description>
    </item>
    <item>
      <title>std::lock_guard</title>
      <dc:creator>Rondale Sidbury</dc:creator>
      <pubDate>Sat, 04 Apr 2026 05:59:46 +0000</pubDate>
      <link>https://dev.to/milez-aac6b7eb9/stdlockguard-54em</link>
      <guid>https://dev.to/milez-aac6b7eb9/stdlockguard-54em</guid>
      <description>&lt;p&gt;&lt;em&gt;{&lt;br&gt;
  "response": "Here is the bug and how to fix it!\n\n### The Bug: Data Race\nThe bug is in &lt;code&gt;counter.cpp&lt;/code&gt; inside the &lt;code&gt;increment()&lt;/code&gt; method:\n&lt;br&gt;
&lt;br&gt;
&lt;code&gt;cpp\nvoid Counter::increment() {\n    value++; // INTENTIONAL BUG\n}\n&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
\nThe &lt;code&gt;value++&lt;/code&gt; operation is not a single, indivisible step. It actually takes three steps at the CPU level:\n1. Read the current value from memory into a register.\n2. Increment the value in the register.\n3. Write the new value back to memory.\n\nWhen 100 threads do this at the exact same time, Thread A and Thread B might both read the value &lt;code&gt;50&lt;/code&gt;. Both increment it to &lt;code&gt;51&lt;/code&gt;, and both write &lt;code&gt;51&lt;/code&gt; back to memory. Two increments happened, but the counter only went up by one. This results in the final count being much lower than expected.\n\n### The Fix\nThere are two standard ways to fix this in C++. For a simple counter, using &lt;code&gt;std::atomic&lt;/code&gt; is the most efficient and cleanest approach. \n\n#### Solution 1: Using &lt;code&gt;std::atomic&lt;/code&gt; (Recommended)\n&lt;code&gt;std::atomic&lt;/code&gt; guarantees that operations on the variable happen as a single, indivisible (atomic) operation. \n\n&lt;/em&gt;&lt;em&gt;Update &lt;code&gt;counter.h&lt;/code&gt;:&lt;/em&gt;&lt;em&gt;\n&lt;br&gt;
&lt;br&gt;
&lt;code&gt;cpp\n#ifndef COUNTER_H\n#define COUNTER_H\n#include &amp;lt;atomic&amp;gt; // Include atomic library\n\nclass Counter {\npublic:\n    Counter();\n    void increment();\n    int getValue() const;\nprivate:\n    std::atomic&amp;lt;int&amp;gt; value; // Change int to std::atomic&amp;lt;int&amp;gt;\n};\n\n#endif // COUNTER_H\n&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
\n\n&lt;/em&gt;&lt;em&gt;Update &lt;code&gt;counter.cpp&lt;/code&gt;:&lt;/em&gt;&lt;em&gt;\n&lt;/em&gt;(No changes needed!)* &lt;code&gt;std::atomic&lt;/code&gt; overloads the &lt;code&gt;++&lt;/code&gt; operator to be thread-safe automatically.\n&lt;br&gt;
&lt;br&gt;
&lt;code&gt;cpp\n#include \"counter.h\"\n\nCounter::Counter() : value(0) {}\n\nvoid Counter::increment() {\n    value++; // This is now completely thread-safe!\n}\n\nint Counter::getValue() const {\n    return value;\n}\n&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
\n\n#### Solution 2: Using &lt;code&gt;std::mutex&lt;/code&gt; (Good for complex logic)\nIf your increment logic was more complex (e.g., checking multiple variables at once), you would use a mutex (mutual exclusion) to lock the section of code so only one thread can execute it at a time.\n\n*&lt;em&gt;Update &lt;code&gt;counter.h&lt;/code&gt;:&lt;/em&gt;&lt;em&gt;\n&lt;br&gt;
&lt;br&gt;
&lt;code&gt;cpp\n#include &amp;lt;mutex&amp;gt;\n// ... inside class Counter:\nprivate:\n    int value;\n    mutable std::mutex mtx; // Add a mutex\n&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
\n\n&lt;/em&gt;&lt;em&gt;Update &lt;code&gt;counter.cpp&lt;/code&gt;:&lt;/em&gt;&lt;em&gt;\n&lt;br&gt;
&lt;br&gt;
&lt;code&gt;cpp\nvoid Counter::increment() {\n    // Locks the mutex. When the function ends, the lock is automatically released.\n    std::lock_guard&amp;lt;std::mutex&amp;gt; lock(mtx); \n    value++; \n}\n&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
\n\nIf you apply either of these fixes, rebuild your code, and run &lt;code&gt;ctest&lt;/code&gt; again, the GoogleTest suite will output &lt;code&gt;[  PASSED  ] 1 test.&lt;/code&gt; because all 1,000,000 increments will be successfully counted!"&lt;br&gt;
}This is a submission for the &lt;a href="https://dev.to/challenges/aprilfools-2026"&gt;DEV April Fools Challenge&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Built
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;h2&gt;
  
  
  How I Built It
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Prize Category
&lt;/h2&gt;

</description>
      <category>devchallenge</category>
      <category>418challenge</category>
      <category>showdev</category>
      <category>python</category>
    </item>
  </channel>
</rss>
