{"id":733,"date":"2023-10-07T17:29:36","date_gmt":"2023-10-07T17:29:36","guid":{"rendered":"https:\/\/baldsolutions.com\/?p=733"},"modified":"2023-10-28T08:45:38","modified_gmt":"2023-10-28T08:45:38","slug":"1-synchronization-primitives-in-net-interlocked-class","status":"publish","type":"post","link":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/","title":{"rendered":"#1 Synchronization primitives in .NET &#8211; Interlocked class"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Introduction<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required.  .NET offers many synchronization primitives that user can utilise according to a particular need. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post I present one of the synchronization primitives &#8211; Interlocked class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is a first post of the synchronization primitives series. I plan to add more posts to describe other primitives that .NET offers (e.g. Mutex, SemaphoreSlim, Barrier etc.).<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Interlocked class<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.interlocked?view=net-7.0\" target=\"_blank\" rel=\"noopener\" title=\"\">Interlocked <\/a>class is a static class that provides atomic operations for variables that are shared by multiple threads (as per <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.interlocked?view=net-7.0\" target=\"_blank\" rel=\"noopener\" title=\"\">docs<\/a>). Let&#8217;s focus on a simple incrementation operation and the following example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>we have a global variable named <code>counter<\/code>,<\/li>\n\n\n\n<li>we run many threads at the same time that increment the <code>counter<\/code> value.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">At first everything looks fine as incrementing the <code>counter<\/code> looks like something that cannot be messed up in terms of accessing by many threads &#8211; but it is not true. As you can read in the <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.interlocked?view=net-7.0#remarks\" target=\"_blank\" rel=\"noopener\" title=\"\">docs<\/a> &#8211; on most computers incrementing is not an atomic operation and consists of three steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>load a <code>counter<\/code> value into a register,<\/li>\n\n\n\n<li>increment the value,<\/li>\n\n\n\n<li>store a value in the <code>counter<\/code>  variable. <\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see instead of one (apparently) one atomic operation there are tree steps that happen behind the scene. Thread can be preempted after executing the first two steps, then another thread executes all three steps and at the end first thread finishes the last step. In this case instead of having the <code>counter + 2<\/code> result the result is <code>counter + 1<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"315\" src=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-6.png\" alt=\"\" class=\"wp-image-750\" srcset=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-6.png 680w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-6-300x139.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s consider the source code from my <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/blob\/master\/dotnet\/synchronization-primitives\/Program.cs\" target=\"_blank\" rel=\"noopener\" title=\"\">GitHub<\/a>. As you can see the <code>StandardIncrement<\/code> method increments passed argument using <code>++<\/code> operator whereas the <code>InterlockedIncrement<\/code> method uses the <code>Interlocked.Increment<\/code>. If you run the provided code using <code>dotnet run -c Release<\/code> command you might see a similar output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>StandardIncrement: 9985\nInterlockedIncrement: 10000<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Behind the scene<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see what are the ASM instructions for the source code. For this purpose we will use <a href=\"https:\/\/sharplab.io\/\" target=\"_blank\" rel=\"noopener\" title=\"\">sharplab<\/a> &#8211; interesting part of the output is highlighted:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"954\" height=\"653\" src=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-9.png\" alt=\"\" class=\"wp-image-753\" srcset=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-9.png 954w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-9-300x205.png 300w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-9-768x526.png 768w\" sizes=\"auto, (max-width: 954px) 100vw, 954px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">What we can see is that these two methods (while doing the same) are represented by  slightly different instructions. The <code>InterlockedIncrement <\/code>method uses the LOCK prefix which makes the instruction atomic whereas the <code>StandardIncrement<\/code> does not (interesting explanation in the <a href=\"https:\/\/stackoverflow.com\/questions\/10109679\/how-come-inc-instruction-of-x86-is-not-atomic\" target=\"_blank\" rel=\"noopener\" title=\"\">link<\/a>).<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Summary<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">In this post I presented and explained the <code>Interlocked<\/code> synchronization primitive in the .NET environment.  The <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.interlocked?view=net-7.0#methods\" target=\"_blank\" rel=\"noopener\" title=\"\">API <\/a> of the<code>Interlocked<\/code> class consists of many methods and I encourage you to give it a try on your own. As always you can find source code on my <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/dotnet\/synchronization-primitives\" target=\"_blank\" rel=\"noopener\" title=\"\">GitHub <\/a>repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Have a nice day, bye!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required. .NET offers many synchronization primitives that user can&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/\">Continue reading<span class=\"screen-reader-text\">#1 Synchronization primitives in .NET &#8211; Interlocked class<\/span><\/a><\/div>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,16],"tags":[],"class_list":["post-733","post","type-post","status-publish","format-standard","hentry","category-net","category-synchronization-primitives","entry"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.8 - aioseo.com -->\n\t<meta name=\"description\" content=\"Introduction To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required. .NET offers many synchronization primitives that user can utilise according to a particular need. In this post I present one of the synchronization primitives - Interlocked class. This is a first post of the synchronization primitives series. I\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Tomasz\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.8\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Bald Solutions -\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"#1 Synchronization primitives in .NET \u2013 Interlocked class - Bald Solutions\" \/>\n\t\t<meta property=\"og:description\" content=\"Introduction To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required. .NET offers many synchronization primitives that user can utilise according to a particular need. In this post I present one of the synchronization primitives - Interlocked class. This is a first post of the synchronization primitives series. I\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2023-10-07T17:29:36+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2023-10-28T08:45:38+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"#1 Synchronization primitives in .NET \u2013 Interlocked class - Bald Solutions\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Introduction To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required. .NET offers many synchronization primitives that user can utilise according to a particular need. In this post I present one of the synchronization primitives - Interlocked class. This is a first post of the synchronization primitives series. I\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#blogposting\",\"name\":\"#1 Synchronization primitives in .NET \\u2013 Interlocked class - Bald Solutions\",\"headline\":\"#1 Synchronization primitives in .NET &#8211; Interlocked class\",\"author\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/image-6.png\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#articleImage\",\"width\":680,\"height\":315},\"datePublished\":\"2023-10-07T17:29:36+00:00\",\"dateModified\":\"2023-10-28T08:45:38+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#webpage\"},\"articleSection\":\".NET, Synchronization Primitives\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/baldsolutions.com\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/category\\\/net\\\/#listItem\",\"name\":\".NET\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/category\\\/net\\\/#listItem\",\"position\":2,\"name\":\".NET\",\"item\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/category\\\/net\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#listItem\",\"name\":\"#1 Synchronization primitives in .NET &#8211; Interlocked class\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#listItem\",\"position\":3,\"name\":\"#1 Synchronization primitives in .NET &#8211; Interlocked class\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/category\\\/net\\\/#listItem\",\"name\":\".NET\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#organization\",\"name\":\"Bald Solutions\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/#author\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/\",\"name\":\"Tomasz\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d72f2f7f389921b820e5ef7c2880172fc2b9ae47698a933356df224581de0cc7?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Tomasz\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#webpage\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/\",\"name\":\"#1 Synchronization primitives in .NET \\u2013 Interlocked class - Bald Solutions\",\"description\":\"Introduction To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required. .NET offers many synchronization primitives that user can utilise according to a particular need. In this post I present one of the synchronization primitives - Interlocked class. This is a first post of the synchronization primitives series. I\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/2023\\\/10\\\/07\\\/1-synchronization-primitives-in-net-interlocked-class\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/index.php\\\/author\\\/tomasz3396\\\/#author\"},\"datePublished\":\"2023-10-07T17:29:36+00:00\",\"dateModified\":\"2023-10-28T08:45:38+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#website\",\"url\":\"https:\\\/\\\/baldsolutions.com\\\/\",\"name\":\"Bald Solutions\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/baldsolutions.com\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"#1 Synchronization primitives in .NET \u2013 Interlocked class - Bald Solutions","description":"Introduction To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required. .NET offers many synchronization primitives that user can utilise according to a particular need. In this post I present one of the synchronization primitives - Interlocked class. This is a first post of the synchronization primitives series. I","canonical_url":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#blogposting","name":"#1 Synchronization primitives in .NET \u2013 Interlocked class - Bald Solutions","headline":"#1 Synchronization primitives in .NET &#8211; Interlocked class","author":{"@id":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/#author"},"publisher":{"@id":"https:\/\/baldsolutions.com\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-6.png","@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#articleImage","width":680,"height":315},"datePublished":"2023-10-07T17:29:36+00:00","dateModified":"2023-10-28T08:45:38+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#webpage"},"isPartOf":{"@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#webpage"},"articleSection":".NET, Synchronization Primitives"},{"@type":"BreadcrumbList","@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/baldsolutions.com#listItem","position":1,"name":"Home","item":"https:\/\/baldsolutions.com","nextItem":{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/category\/net\/#listItem","name":".NET"}},{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/category\/net\/#listItem","position":2,"name":".NET","item":"https:\/\/baldsolutions.com\/index.php\/category\/net\/","nextItem":{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#listItem","name":"#1 Synchronization primitives in .NET &#8211; Interlocked class"},"previousItem":{"@type":"ListItem","@id":"https:\/\/baldsolutions.com#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#listItem","position":3,"name":"#1 Synchronization primitives in .NET &#8211; Interlocked class","previousItem":{"@type":"ListItem","@id":"https:\/\/baldsolutions.com\/index.php\/category\/net\/#listItem","name":".NET"}}]},{"@type":"Organization","@id":"https:\/\/baldsolutions.com\/#organization","name":"Bald Solutions","url":"https:\/\/baldsolutions.com\/"},{"@type":"Person","@id":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/#author","url":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/","name":"Tomasz","image":{"@type":"ImageObject","@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/d72f2f7f389921b820e5ef7c2880172fc2b9ae47698a933356df224581de0cc7?s=96&d=mm&r=g","width":96,"height":96,"caption":"Tomasz"}},{"@type":"WebPage","@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#webpage","url":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/","name":"#1 Synchronization primitives in .NET \u2013 Interlocked class - Bald Solutions","description":"Introduction To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required. .NET offers many synchronization primitives that user can utilise according to a particular need. In this post I present one of the synchronization primitives - Interlocked class. This is a first post of the synchronization primitives series. I","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/baldsolutions.com\/#website"},"breadcrumb":{"@id":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/#breadcrumblist"},"author":{"@id":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/#author"},"creator":{"@id":"https:\/\/baldsolutions.com\/index.php\/author\/tomasz3396\/#author"},"datePublished":"2023-10-07T17:29:36+00:00","dateModified":"2023-10-28T08:45:38+00:00"},{"@type":"WebSite","@id":"https:\/\/baldsolutions.com\/#website","url":"https:\/\/baldsolutions.com\/","name":"Bald Solutions","inLanguage":"en-US","publisher":{"@id":"https:\/\/baldsolutions.com\/#organization"}}]},"og:locale":"en_US","og:site_name":"Bald Solutions -","og:type":"article","og:title":"#1 Synchronization primitives in .NET \u2013 Interlocked class - Bald Solutions","og:description":"Introduction To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required. .NET offers many synchronization primitives that user can utilise according to a particular need. In this post I present one of the synchronization primitives - Interlocked class. This is a first post of the synchronization primitives series. I","og:url":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/","article:published_time":"2023-10-07T17:29:36+00:00","article:modified_time":"2023-10-28T08:45:38+00:00","twitter:card":"summary","twitter:title":"#1 Synchronization primitives in .NET \u2013 Interlocked class - Bald Solutions","twitter:description":"Introduction To coordinate threads interaction when accessing a shared resource some kind of synchronization mechanism is required. .NET offers many synchronization primitives that user can utilise according to a particular need. In this post I present one of the synchronization primitives - Interlocked class. This is a first post of the synchronization primitives series. I"},"aioseo_meta_data":{"post_id":"733","title":null,"description":null,"keywords":[],"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2023-10-07 15:20:15","updated":"2025-06-04 05:28:42","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/baldsolutions.com\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/baldsolutions.com\/index.php\/category\/net\/\" title=\".NET\">.NET<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t#1 Synchronization primitives in .NET \u2013 Interlocked class\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/baldsolutions.com"},{"label":".NET","link":"https:\/\/baldsolutions.com\/index.php\/category\/net\/"},{"label":"#1 Synchronization primitives in .NET &#8211; Interlocked class","link":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/"}],"_links":{"self":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/comments?post=733"}],"version-history":[{"count":13,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/733\/revisions"}],"predecessor-version":[{"id":780,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/733\/revisions\/780"}],"wp:attachment":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/media?parent=733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/categories?post=733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/tags?post=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}