<?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 Codrut Constantin Gusoi</title>
    <link>https://www.codrut.pro/tags/concurrency/</link>
    <description>Recent content in Concurrency on Codrut Constantin Gusoi</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Mon, 04 Jan 2021 23:30:24 +0000</lastBuildDate>
    <atom:link href="https://www.codrut.pro/tags/concurrency/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Concurrency primitives in Ruby</title>
      <link>https://www.codrut.pro/snippets/concurrency-primitives-in-ruby/</link>
      <pubDate>Mon, 04 Jan 2021 23:30:24 +0000</pubDate>
      <guid>https://www.codrut.pro/snippets/concurrency-primitives-in-ruby/</guid>
      <description>&lt;blockquote&gt;&#xA;&lt;p&gt;Note: for parallelism, see &lt;a href=&#34;https://www.codrut.pro/snippets/true-parallelism-in-ruby/&#34;&gt;Ractors&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&#xA;&lt;p&gt;There&amp;rsquo;s 3 of them:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Thread&lt;/li&gt;&#xA;&lt;li&gt;Mutex&lt;/li&gt;&#xA;&lt;li&gt;Condition Variable&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;thread&#34;&gt;&#xA;  Thread&#xA;  &lt;a class=&#34;heading-link&#34; href=&#34;#thread&#34;&gt;&#xA;    &lt;i class=&#34;fa fa-link&#34; aria-hidden=&#34;true&#34; title=&#34;Link to heading&#34;&gt;&lt;/i&gt;&#xA;    &lt;span class=&#34;sr-only&#34;&gt;Link to heading&lt;/span&gt;&#xA;  &lt;/a&gt;&#xA;&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;first  &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Thread&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new { puts &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Thread 1&amp;#39;&lt;/span&gt;; sleep &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;second &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Thread&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new { puts &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Thread 2&amp;#39;&lt;/span&gt;; sleep &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;third  &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Thread&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;new { puts &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Thread 3&amp;#39;&lt;/span&gt;; sleep &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;puts &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Main Thread&amp;#39;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;first, second, third&lt;span style=&#34;color:#f92672&#34;&gt;].&lt;/span&gt;map(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:join&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Takeaways:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Blocks execute concurrently, but not in parallel.&lt;/li&gt;&#xA;&lt;li&gt;Pause a thread for a few seconds with &lt;code&gt;#sleep&lt;/code&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Wait for threads to finish execution with the &lt;code&gt;#join&lt;/code&gt; method.&lt;/li&gt;&#xA;&lt;li&gt;Execution scheduling is non deterministic, output will have a different order&#xA;over multiple runs.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;mutex&#34;&gt;&#xA;  Mutex&#xA;  &lt;a class=&#34;heading-link&#34; href=&#34;#mutex&#34;&gt;&#xA;    &lt;i class=&#34;fa fa-link&#34; aria-hidden=&#34;true&#34; title=&#34;Link to heading&#34;&gt;&lt;/i&gt;&#xA;    &lt;span class=&#34;sr-only&#34;&gt;Link to heading&lt;/span&gt;&#xA;  &lt;/a&gt;&#xA;&lt;/h2&gt;&#xA;&lt;p&gt;Syncronize access to a shared resource with a &lt;code&gt;Mutex&lt;/code&gt;:&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
