<?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: decillion</title>
    <description>The latest articles on DEV Community by decillion (@decillion).</description>
    <link>https://dev.to/decillion</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%2F43173%2Feee6ee27-ebaf-46b2-9015-c91df85f038c.png</url>
      <title>DEV Community: decillion</title>
      <link>https://dev.to/decillion</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/decillion"/>
    <language>en</language>
    <item>
      <title>Software Transactional Memory in Go</title>
      <dc:creator>decillion</dc:creator>
      <pubDate>Sun, 24 Dec 2017 19:47:22 +0000</pubDate>
      <link>https://dev.to/decillion/software-transactional-memory-in-go-2g</link>
      <guid>https://dev.to/decillion/software-transactional-memory-in-go-2g</guid>
      <description>&lt;p&gt;I have recently written &lt;a href="https://github.com/decillion/go-stm/"&gt;decillion/go-stm&lt;/a&gt;, which is a software transactional memory (STM) implementation for Go. It provides a new way of synchronization to Go programmers, while it is still experimental one. &lt;/p&gt;

&lt;p&gt;The package enables one to atomically execute a sequence of operations on shared variables without explicit locking. I present a simple example below. Please see the repository or a &lt;a href="https://qiita.com/decillion/items/d5da905e28b54dc769cd"&gt;blog post&lt;/a&gt; (in Japanese) for further information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;
&lt;span class="c"&gt;// Initialize two transactional variables.&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// Define a transaction that transfer 100 from x to y.&lt;/span&gt;
&lt;span class="n"&gt;transfer&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rec&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;TRec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;currX&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Read the value of x.&lt;/span&gt;
    &lt;span class="n"&gt;currY&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Read the value of y.&lt;/span&gt;
    &lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;currX&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c"&gt;// Write currX-100 to x.&lt;/span&gt;
    &lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;currY&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c"&gt;// Write currY-100 to y.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Execute the transaction atomically.&lt;/span&gt;
&lt;span class="n"&gt;Atomically&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to know more about the STM itself, I recommend you to read the article, &lt;a href="https://www.microsoft.com/en-us/research/publication/beautiful-concurrency/"&gt;Beautiful concurrency&lt;/a&gt;, written by Simon Peyton Jones.&lt;/p&gt;

&lt;p&gt;There has been &lt;a href="https://github.com/lukechampine/stm"&gt;another STM package&lt;/a&gt; written by lukechampine. It provides a richer STM interface but is less performant than the present package. Here is the result of a very simple benchmark, in which two transactional variables are atomically incremented or read. The benchmark is taken at Ubuntu 17.10 on a DigitalOcean's High CPU Droplet with 32 cores.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Benchmark_Read90Write10_decillion-4         10000000           156 ns/op
Benchmark_Read90Write10_decillion-8         10000000           144 ns/op
Benchmark_Read90Write10_decillion-16        10000000           214 ns/op
Benchmark_Read90Write10_decillion-32         5000000           289 ns/op

Benchmark_Read90Write10_lukechampine-4       2000000           761 ns/op
Benchmark_Read90Write10_lukechampine-8       2000000           822 ns/op
Benchmark_Read90Write10_lukechampine-16      2000000           912 ns/op
Benchmark_Read90Write10_lukechampine-32      2000000           966 ns/op
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>go</category>
      <category>stm</category>
      <category>concurrency</category>
    </item>
  </channel>
</rss>
