{"id":308205,"date":"2021-07-26T12:00:37","date_gmt":"2021-07-26T09:00:37","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-a-race-condition-cloudsavvy-it\/"},"modified":"2021-07-26T12:00:37","modified_gmt":"2021-07-26T09:00:37","slug":"what-is-a-race-condition-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-a-race-condition-cloudsavvy-it\/","title":{"rendered":"#What Is a Race Condition? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a28a3850816a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a28a3850816a\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-a-race-condition-cloudsavvy-it\/#What_Are_Computer_Threads\" >What Are Computer Threads?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-a-race-condition-cloudsavvy-it\/#What_Is_a_Race_Condition\" >What Is a Race Condition?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-a-race-condition-cloudsavvy-it\/#Preventing_Race_Conditions_Thread_Safety\" >Preventing Race Conditions: Thread Safety<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-a-race-condition-cloudsavvy-it\/#Wrapping_up\" >Wrapping up<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Is a Race Condition? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage wp-image-12952 size-full\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/712dcf17.jpg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/712dcf17.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/712dcf17.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"race baton\" width=\"1200\" height=\"675\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-photo\/close-male-athlete-passing-relay-baton-1855878205\" data-credittext=\"Juice Dash\/Shutterstock.com\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/close-male-athlete-passing-relay-baton-1855878205\">Juice Dash\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>Have you ever run a race? If so, you already know that a photo finish is sometimes required to pick the winner! But what if two people arrived at the same time at the finish line? Welcome to race conditions.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Are_Computer_Threads\"><\/span>What Are <i>Computer Threads<\/i>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To explain a race condition, we first have to understand a little about how computers work internally. When you use an operating system, you will take various actions like opening a command terminal window, opening a browser, etc. Each of those actions will result in a reaction by the operating system to start a new computing\/computer thread.<\/p>\n<p>A thread is a computing\/computer process that will execute\/run the various steps (programming steps, originally written in source code format and regularly compiled by a compiler) required to execute the task you asked the operating system or software running on it to perform.<\/p>\n<p>In Linux, such a thread is uniquely identified by a <i>PID<\/i> (Process Identifier). To learn more about PID\u2019s in Linux, you can read our articles Bash Automation and <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a>ing Basics (Part 3) and How Linux Signals Work: SIGINT, SIGTERM, and SIGKILL.<\/p>\n<p>In Windows, a thread is also uniquely identified by a process ID (Ref the <i>PID<\/i> column in Windows Task Manager), though the implementation of the process handling is different between Linux and Windows; different underlying code, different <i>PID<\/i> interaction tools etc. and limited compatibility. Also, the Windows process ID <i>PID<\/i> is not be confused with Product ID <i>PID<\/i> (same term, different meaning) or <i>VID<\/i> (Vendor ID). The latter two refer to the identification of devices and are unrelated to process management.<\/p>\n<p>When a thread starts, it can in itself start other threads. The original thread is often referred to as the main or the parent thread. For example, when you click the icon of your favorite web browser, it will im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely start a thread (the main thread), and that thread will very quickly start several subthreads, or child threads, and thus become the parent thread.<\/p>\n<p>You may also think about threads like runners in a race. For example, think about a busy database server serving many different connected clients. Each of those client threads (note the use of the word thread) will (in many cases) in and by itself have at least one thread on the database host server and\/or inside the database software itself (i.e., two threads, one logged into the operating system and one inside the database software).<\/p>\n<p>The database server is trying to serve all of those threads at the same time \u2013 hence the term <i>concurrent processes<\/i> or <i>concurrent threads<\/i> and if there are bugs in the database software (or operating system, etc.) then sooner or later it may run into a race condition.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Is_a_Race_Condition\"><\/span>What Is a <i>Race Condition<\/i>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A simple way to relate this to runners running in a race is to picture a photo finish where two runners truly cross the finish line at the same point in time. It is possible, though quite unlikely, for this to occur in human races. For computers that process thousands of operations per millisecond, it becomes a lot more feasible.<\/p>\n<p>As another example, picture a relay race where runners pass a baton (the flashy colored stick) from one person to the next. Imagine now that one of the race participants makes a mistake, and there are now two runners who think they should get the red-colored baton.<\/p>\n<p>A significant event in a relay race is the passing on of the baton, as this may mean that the previous holder of the baton can stop running, and it\u2019s now up to the new baton owner to give their best. There are now two runners grabbing for the baton. It\u2019s going to be an interesting situation to watch on TV (if you like that sort of thing), but it is clear there is going to be some amount of fallout.<\/p>\n<p>In essence, a race condition is a bug, error, or flaw in computer system code which produces unpredictable results: an unexpected sequence of events. It is normally caused by two threads conflicting in some way through more than two threads may be involved in the actual conflict, and often more than two threads are running in the software faulting.<\/p>\n<p>In our human race example, we had two people accessing an object at approximately the same time, and the <i>corruption<\/i> (a computer term to indicate that some data was corrupted, where such data could reside in memory or on disk or in the CPU, etc.) happened to take place at the moment where two people (or two threads in computer analogy) tried to grab the baton and conflict occurred. In computer terms, two threads tried to write a memory space which should normally only be written by one thread (one runner).<\/p>\n<p>Race conditions can happen in various areas like inside electronics, in computer software, and <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> life. For example, a call collision is a telecommunications term to describe the situation where a communications channel is seized at both ends simultaneously. Inside computer software \u2013 one of the most prominent areas of race conditions \u2013 there are a wide variety of race conditions possible.<\/p>\n<p>As another example of a race condition inside computer software, picture two computing threads working with a given memory space. A user has just committed a form, and the backend software is writing this form into memory. Simultaneously, another user is reading out the fields of this form from the same memory space. Depending on what happens, the reading user may receive a partially incorrect form with partially updated information.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Preventing_Race_Conditions_Thread_Safety\"><\/span>Preventing Race Conditions: <i>Thread Safety<\/i><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There has been much discussion around race conditions in the IT industry. Depending on the coding language you use, there may be extensive, or few, provisions for handling race conditions. An often-used term is <i>thread safety<\/i> or a <i>thread safe<\/i> application or programming language [construct]. Such terms are used to indicate whether a piece of code or software as a whole is <i>thread safe<\/i>, i.e., written in such a way as to avoid and even prevent race conditions.<\/p>\n<p>If software is deemed <i>thread safe<\/i>, it is deemed to be free of the possibility of race conditions. In many cases, \u2018<i>deemed<\/i>\u2018 thread-safe is the best developers can deliver, and all the more so when many threads and interactions are possible. The complexity of many threads working with many resources can easily become a myriad of code handling and an even larger myriad of possible race conditions.<\/p>\n<p>Various programming constructs may be used to prevent race conditions. For example, semaphores and mutexes. The complexity of using such constructs will depend on the programming language being used and their native support for improved thread handling. For example, in C++ one may look at the <i>std::mutex<\/i> class for implementing a mutex (i.e. <i>mutually exclusive<\/i>) lock. In Bash, however, one does not find such a construct natively.<\/p>\n<p>Stepping further, one may also consider which particular constructs, functions, or even executables and libraries are thread-safe already, and then use such constructs, functions, executables, and libraries as a base for building a new construct, function, executable, library, or full software package.<\/p>\n<p>Implementing even basic thread-safety handling constructs can be a complex matter. For example, consider the difficulty of implementing a Semaphore in Bash.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Wrapping_up\"><\/span>Wrapping up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this article, we explored computing threads and race conditions. We looked at analogies with running races and relay races in human life to explore some basic race conditions which may happen inside computers. Finally, we explored thread safety, the different implementations of race condition handling in computer coding languages, and how we may prevent race conditions.<\/p>\n<p>If you liked this article, have a look at\u00a0How Logic Gates Work: OR, AND, XOR, NOR, NAND, XNOR, and NOT article.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/12231\/what-is-a-race-condition\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Is a Race Condition? \u2013 CloudSavvy IT&#8221; Juice Dash\/Shutterstock.com Have you ever run a race? If so, you already know that a photo finish is sometimes required to pick the winner! But what if two people arrived at the same time at the finish line? Welcome to race conditions. What Are Computer Threads? To&#8230;<\/p>\n","protected":false},"author":1,"featured_media":308206,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/712dcf17.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-308205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/308205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=308205"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/308205\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/308206"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=308205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=308205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=308205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}