{"id":764,"date":"2023-10-28T08:59:21","date_gmt":"2023-10-28T08:59:21","guid":{"rendered":"https:\/\/baldsolutions.com\/?p=764"},"modified":"2023-10-28T09:55:46","modified_gmt":"2023-10-28T09:55:46","slug":"2-synchronization-primitives-in-net-barrier-class","status":"publish","type":"post","link":"https:\/\/baldsolutions.com\/index.php\/2023\/10\/28\/2-synchronization-primitives-in-net-barrier-class\/","title":{"rendered":"#2 Synchronization primitives in .NET &#8211; Barrier class"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Introduction<\/h1>\n\n\n\n<p>This is a second post of the synchronization primitives series. In the previous <a href=\"https:\/\/baldsolutions.com\/index.php\/2023\/10\/07\/1-synchronization-primitives-in-net-interlocked-class\/\" target=\"_blank\" rel=\"noopener\" title=\"\">post<\/a> you can read about 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 primitive. In this post I describe and present an example regarding the <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0\" target=\"_blank\" rel=\"noopener\" title=\"\">Barrier<\/a> class &#8211; another interesting synchronization primitive that .NET offers.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Barrier class<\/h1>\n\n\n\n<p>The Barrier class is an <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.idisposable?view=net-7.0\" target=\"_blank\" rel=\"noopener\" title=\"\">IDisposable<\/a> and non-static class that enables multiple tasks to cooperatively work on an algorithm in parallel through multiple phases (as in the definition in <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0#definition\" target=\"_blank\" rel=\"noopener\" title=\"\">docs<\/a>) .  Once you create a <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0\" target=\"_blank\" rel=\"noopener\" title=\"\">Barrier<\/a> class object you can access its all public and protected members (apart from the Dispose method) in a thread-safe manner (<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0#thread-safety\" target=\"_blank\" rel=\"noopener\" title=\"\">docs<\/a>). <\/p>\n\n\n\n<p>Usage scenario of the <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0\" target=\"_blank\" rel=\"noopener\" title=\"\">Barrier<\/a> class can look like this:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0\" target=\"_blank\" rel=\"noopener\" title=\"\">Barrier<\/a> object by passing arguments to a constructor:\n<ul class=\"wp-block-list\">\n<li>participantCount &#8211; how many  threads participate in a phase.<\/li>\n\n\n\n<li>postPhaseAction (optional) &#8211; an action that is invoked  after each phase.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Define an algorithm consisting of multiple phases. Ideally each phase should be executed in parallel by multiple threads. If a thread finishes its job in a phase it invokes one of the <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0#methods\" target=\"_blank\" rel=\"noopener\" title=\"\">SignalAndWait<\/a> overload &#8211; it means &#8220;The thread finished and waits for other threads to finish so that a phase can be finished&#8221;. Phase (N+1) can not be started if the Phase N is not finished.<\/li>\n\n\n\n<li>Define N threads and start the algorithm.<\/li>\n<\/ol>\n\n\n\n<p>Let me show the real code example to ease the understanding of the steps.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Example<\/h1>\n\n\n\n<p>Let&#8217;s define a problem to solve: &#8220;You are asked to find 7 Guids so that the two leading bytes of SHA256 of each Guid are zeros and store the Guids and hashes in a collection. Then find another 7 different Guids so that the three leading bytes of SHA256 of each Guid are zeros and store the Guids and hashes in another collection&#8221;.<\/p>\n\n\n\n<p>As you can see the probles consist of two phases:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Find 7 Guids so that hashes have two leading zeros,<\/li>\n\n\n\n<li>Find another 7 Guids so that hashes have three leading zeros.<\/li>\n<\/ul>\n\n\n\n<p>Moreover, you can figure out that each phase can be processes by 7 independent threads. Each thread tries to find and store its own Guid and corresponding hash in the first phase and then in the second phase:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"793\" height=\"266\" src=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-19.png\" alt=\"\" class=\"wp-image-795\" srcset=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-19.png 793w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-19-300x101.png 300w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-19-768x258.png 768w\" sizes=\"auto, (max-width: 793px) 100vw, 793px\" \/><\/figure>\n\n\n\n<p>Once the problem is defined let&#8217;s jump to the code.<\/p>\n\n\n\n<p>Create a <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0#definition\" target=\"_blank\" rel=\"noopener\" title=\"\">Barrier<\/a> object:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"727\" src=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-13-1024x727.png\" alt=\"\" class=\"wp-image-774\" srcset=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-13-1024x727.png 1024w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-13-300x213.png 300w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-13-768x545.png 768w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-13.png 1103w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Define an algorithm consisting of multiple phases:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1018\" height=\"755\" src=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-14.png\" alt=\"\" class=\"wp-image-775\" srcset=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-14.png 1018w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-14-300x222.png 300w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-14-768x570.png 768w\" sizes=\"auto, (max-width: 1018px) 100vw, 1018px\" \/><\/figure>\n\n\n\n<p>Define N threads and start the algorithm:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"950\" height=\"600\" src=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-15.png\" alt=\"\" class=\"wp-image-776\" srcset=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-15.png 950w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-15-300x189.png 300w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-15-768x485.png 768w\" sizes=\"auto, (max-width: 950px) 100vw, 950px\" \/><\/figure>\n\n\n\n<p>If you run the program you would see the following output (it might take ~1 minute to compute second collection of hashes so be patient):<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"382\" src=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-16.png\" alt=\"\" class=\"wp-image-777\" srcset=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-16.png 840w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-16-300x136.png 300w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2023\/10\/image-16-768x349.png 768w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p>As you can see the first phase (Phase0) started and computed all 7 hashes with two leading bytes equal zero (2 zero bytes equals &#8220;0000&#8221; in hex hence there is &#8220;0000&#8221; in Phase0 and &#8220;000000&#8221; in Phase1). Once the Phase0 is finished the Phase1 started and computed its own 7 hashes with three leading bytes equal zero. <\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Summary<\/h1>\n\n\n\n<p>In this post I presented and explained the <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0#definition\" target=\"_blank\" rel=\"noopener\" title=\"\">Barrier<\/a> synchronization primitive in the .NET environment. The API of the <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0#definition\" target=\"_blank\" rel=\"noopener\" title=\"\">Barrier<\/a> class consists of members I did not mention in the post (see <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0#properties\" target=\"_blank\" rel=\"noopener\" title=\"\">Properties<\/a> and <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.barrier?view=net-7.0#methods\" target=\"_blank\" rel=\"noopener\" title=\"\">Methods<\/a> sections) and I strongly 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>Have a nice day, bye!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction This is a second post of the synchronization primitives series. In the previous post you can read about the Interlocked class primitive. In this&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/baldsolutions.com\/index.php\/2023\/10\/28\/2-synchronization-primitives-in-net-barrier-class\/\">Continue reading<span class=\"screen-reader-text\">#2 Synchronization primitives in .NET &#8211; Barrier 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-764","post","type-post","status-publish","format-standard","hentry","category-net","category-synchronization-primitives","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/764","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=764"}],"version-history":[{"count":28,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":808,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/764\/revisions\/808"}],"wp:attachment":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}