<?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>Forem: I P U L   D E V</title>
    <description>The latest articles on Forem by I P U L   D E V (@ipuldev).</description>
    <link>https://forem.com/ipuldev</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%2F978096%2Fe56a66d7-7b7c-4d34-968f-62a85380d3fc.gif</url>
      <title>Forem: I P U L   D E V</title>
      <link>https://forem.com/ipuldev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ipuldev"/>
    <language>en</language>
    <item>
      <title>Goroutine Race Condition</title>
      <dc:creator>I P U L   D E V</dc:creator>
      <pubDate>Fri, 02 Dec 2022 17:14:14 +0000</pubDate>
      <link>https://forem.com/ipuldev/goroutine-race-condition-4p47</link>
      <guid>https://forem.com/ipuldev/goroutine-race-condition-4p47</guid>
      <description>&lt;p&gt;What is race conditions ?...&lt;/p&gt;

&lt;p&gt;A race condition is a condition when more than one goroutine accesses the same data at the same time. Cause of that the data gets messed up, it's common case in concurrency programming.&lt;/p&gt;

&lt;p&gt;How to solve it ?....&lt;/p&gt;

&lt;p&gt;Golang provides a package called &lt;code&gt;sync.mutex&lt;/code&gt;, when the race condition occurs, only one goroutine can access the data, with mutex all goroutines can access data, how it works ? mutex can control data flow using &lt;strong&gt;lock&lt;/strong&gt; and &lt;strong&gt;unlock&lt;/strong&gt; function&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's code&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  A.Validate Race Condition
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1.Initial variable with a function to increment the value
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type counter struct {
    number int
}

func (value *counter) Add(param int) {
    value.number += param
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2.Run the counter function using goroutines and &lt;code&gt;sync.WaitGroup&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import (
    "fmt"
    "sync"
)

func main() {
    var counter counter
    wg := new(sync.WaitGroup)
    for i := 0; i &amp;lt; 100; i++ {
        wg.Add(1)
        go func() {
            for i := 0; i &amp;lt; 100; i++ {
                counter.Add(1)
            }
            wg.Done()
        }()
    }
    wg.Wait()
    fmt.Println(counter.number)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Full code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "fmt"
    "sync"
)

type counter struct {
    number int
}

func (value *counter) Add(param int) {
    value.number += param
}

func main() {
    var counter counter
    wg := new(sync.WaitGroup)
    for i := 0; i &amp;lt; 100; i++ {
        wg.Add(1)
        go func() {
            for i := 0; i &amp;lt; 100; i++ {
                counter.Add(1)
            }
            wg.Done()
        }()
    }
    wg.Wait()
    fmt.Println(counter.number)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;run the code using this command&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;go run -race main.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;add &lt;em&gt;-race&lt;/em&gt; flag is to detect race condition in our goroutine, and the result is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm9m7htifnw16tb5y6e0u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm9m7htifnw16tb5y6e0u.png" alt="Image description" width="189" height="67"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;race condition is detected &lt;strong&gt;how we can solve this problem&lt;/strong&gt;..... ?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's Solve&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this case using &lt;code&gt;sync.mutex&lt;/code&gt; package is the best solution, cause we can lock data when task is begin and unlock data when task is done.&lt;/p&gt;

&lt;h3&gt;
  
  
  B.Solve Race Condition
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1.Add sync.mutex in counter struct
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type counter struct {
    number int
    sync.Mutex
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2.Lock &amp;amp; Unlock data
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (value *counter) Add(param int) {
    value.Lock()
    value.number += param
    value.Unlock()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3.Run Code
&lt;/h4&gt;

&lt;p&gt;No race conditions will be detected and our problem is solved&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;go run -race main.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;result is &lt;code&gt;100000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
&lt;code&gt;sync.Mutex&lt;/code&gt; is one of thread safe type, using this package makes our goroutines safer and far from race conditions&lt;/p&gt;

&lt;p&gt;don't panic &amp;amp; stay safe, thanks &lt;/p&gt;

</description>
      <category>go</category>
      <category>goroutine</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
