<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Concurrency on Umit Unal</title><link>https://umitunal.net/tags/concurrency/</link><description>Recent content in Concurrency on Umit Unal</description><generator>Hugo</generator><language>en-us</language><copyright>© 2020 Umit Unal.</copyright><lastBuildDate>Fri, 09 Apr 2021 12:42:59 +0300</lastBuildDate><atom:link href="https://umitunal.net/tags/concurrency/index.xml" rel="self" type="application/rss+xml"/><item><title>Implementing CountDownLatch Functionality in Go Inspired by Java</title><link>https://umitunal.net/2021/04/implementing-countdownlatch-functionality-in-go-inspired-by-java/</link><pubDate>Fri, 09 Apr 2021 12:42:59 +0300</pubDate><guid>https://umitunal.net/2021/04/implementing-countdownlatch-functionality-in-go-inspired-by-java/</guid><description>&lt;p&gt;When working with concurrent applications, synchronization primitives are essential tools. While Go provides &lt;code&gt;sync.WaitGroup&lt;/code&gt;, sometimes we need more sophisticated control like timeouts. Java&amp;rsquo;s &lt;code&gt;CountDownLatch&lt;/code&gt; offers this functionality, so let&amp;rsquo;s implement it in Go.&lt;/p&gt;
&lt;h2 id="the-implementation"&gt;The Implementation&lt;/h2&gt;
&lt;p&gt;Our CountDownLatch combines Go&amp;rsquo;s &lt;code&gt;sync.WaitGroup&lt;/code&gt; with atomic operations for thread-safe counting:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;CountDownLatch&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;sync&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;WaitGroup&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;counter&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;uint64&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The struct embeds &lt;code&gt;sync.WaitGroup&lt;/code&gt; and adds a &lt;code&gt;counter&lt;/code&gt; field using &lt;code&gt;uint64&lt;/code&gt; to store two 32-bit counters in one atomic value.&lt;/p&gt;</description></item></channel></rss>