{"id":1079,"date":"2020-10-06T10:46:18","date_gmt":"2020-10-06T10:46:18","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1079"},"modified":"2020-10-06T10:46:56","modified_gmt":"2020-10-06T10:46:56","slug":"race-condition","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/10\/06\/race-condition\/","title":{"rendered":"Race condition"},"content":{"rendered":"<p>When two processes are executing concurrently, the exact order of execution of instructions is not deterministic. Without explicit synchronization instructions it is difficult to predict the outcome of these concurrent execution of process.<\/p>\n<h2>Example 1<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc secondFunction() {\n  fmt.Println(\" Second Function\")\n}\n\nfunc main() {\n  go secondFunction()\n  fmt.Print(\" Main function\")\n}<\/code><\/pre>\n\n\n\n<p>As evident in the example above, we define a GO module secondFunction that is called as the first instruction of the main routine.<\/p>\n\n\n\n<p>In some conditions the line \u201cSecond Function\u201d should appear immediately before \u201c Main Function\u201d and in some cases only the \u201c Main function\u201d gets printed.<\/p>\n\n\n\n<p>There is no sure shot way of knowing what will be the exact outcome of the execution. This is due to the race condition, where the CPU cycles are shared between the two threads (interleavings of instructions)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc secondFunction() {\n    fmt.Println(\" Second Function\")\n}\n\nfunc thirdFunction() {\n    fmt.Println(\" Third Function\")\n}\n\nfunc main() {\n    go secondFunction()\n    fmt.Print(\" Main function\\n\")\n    go thirdFunction()\n}<\/code><\/pre>\n\n\n\n<p>\u00a0tried execution three times as evident in the output below.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"902\" height=\"248\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/10\/image.png\" alt=\"\" class=\"wp-image-1080\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/10\/image.png 902w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/10\/image-300x82.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/10\/image-768x211.png 768w\" sizes=\"(max-width: 902px) 100vw, 902px\" \/><\/figure>\n\n\n\n<p>You can see the different output each time, which displays the race condition aptly, as we can never be sure of what will be the output each time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When two processes are executing concurrently, the exact order of execution of instructions is not deterministic. Without explicit synchronization instructions it is difficult to predict the outcome of these concurrent execution of process. Example 1 As evident in the example above, we define a GO module secondFunction that is called as the first instruction of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1081,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[23,114],"class_list":["post-1079","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-golang","tag-race","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1079"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1079\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1081"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}