{"id":61354,"date":"2024-04-24T14:44:35","date_gmt":"2024-04-24T09:14:35","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=61354"},"modified":"2024-04-29T15:25:36","modified_gmt":"2024-04-29T09:55:36","slug":"making-threads-work-together-synchronization","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/making-threads-work-together-synchronization\/","title":{"rendered":"Making Threads Work Together \u2014 Synchronization"},"content":{"rendered":"<p>Picture a kitchen in India with two cooks, Gautam and Badal. They\u2019re both preparing delicious dishes, but there\u2019s a unique spice box they both want to use. However, only one cook can use it at a time.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-61349 alignnone\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/04\/1_UauZvccnUwzH3b9e41NQyw-300x300.webp\" alt=\"\" width=\"478\" height=\"478\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/04\/1_UauZvccnUwzH3b9e41NQyw-300x300.webp 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/1_UauZvccnUwzH3b9e41NQyw-150x150.webp 150w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/1_UauZvccnUwzH3b9e41NQyw-624x624.webp 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/1_UauZvccnUwzH3b9e41NQyw-120x120.webp 120w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/1_UauZvccnUwzH3b9e41NQyw-24x24.webp 24w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/1_UauZvccnUwzH3b9e41NQyw-48x48.webp 48w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/1_UauZvccnUwzH3b9e41NQyw-96x96.webp 96w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/1_UauZvccnUwzH3b9e41NQyw.webp 720w\" sizes=\"(max-width: 478px) 100vw, 478px\" \/><\/p>\n<p id=\"a14b\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\">Here\u2019s how they ensure a flavorful collaboration:<\/p>\n<p id=\"cb71\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong class=\"adb ng\">Diverse Tasks:<\/strong><\/p>\n<ul class=\"\">\n<li id=\"8ce2\" class=\"acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt aed aee aef bj\" data-selectable-paragraph=\"\">Gautam is making biryani, and Badal is crafting curry. Both tasks require a special spice box.<\/li>\n<\/ul>\n<p id=\"c863\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong class=\"adb ng\">Single Spice Box:<\/strong><\/p>\n<ul class=\"\">\n<li id=\"3a8a\" class=\"acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt aed aee aef bj\" data-selectable-paragraph=\"\">There\u2019s only one spice box, and they need to share it.<\/li>\n<\/ul>\n<p id=\"f3aa\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong class=\"adb ng\">Alternating Turns:<\/strong><\/p>\n<ul class=\"\">\n<li id=\"8a0e\" class=\"acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt aed aee aef bj\" data-selectable-paragraph=\"\">They decide to take turns. If Gautam uses the spice box, Badal patiently waits, and vice versa.<\/li>\n<\/ul>\n<p id=\"0035\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong class=\"adb ng\">Effective Communication:<\/strong><\/p>\n<ul class=\"\">\n<li id=\"fca0\" class=\"acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt aed aee aef bj\" data-selectable-paragraph=\"\">Gautam and Badal communicate well. When one finishes, they inform the other, preventing any spice-related chaos.<\/li>\n<\/ul>\n<p id=\"6dbd\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong class=\"adb ng\">Harmonious Cooking:<\/strong><\/p>\n<ul class=\"\">\n<li id=\"114c\" class=\"acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt aed aee aef bj\" data-selectable-paragraph=\"\">This synchronized approach creates a feast without overwhelming the spice box. It\u2019s like a coordinated dance in the lively kitchen of flavors.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><em>This scenario mirrors thread synchronization in the programming world, particularly with threads. <a class=\"af ol\" href=\"https:\/\/medium.com\/stackademic\/speed-boost-how-multi-threading-makes-your-programs-faster-fc663e27d98c\" rel=\"noopener\">Threads<\/a>, like Gautam and Badal, work together, taking turns to access shared resources and ensuring a harmonious process.<\/em><\/p>\n<p>Let\u2019s talk about how threads can play nicely together in Python. It\u2019s like teaching them to take turns to work together without causing a mess.<\/p>\n<p id=\"5c42\" class=\"acz ada aej adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><em>Basic knowledge of\u00a0<a class=\"af ol\" href=\"https:\/\/medium.com\/p\/fc663e27d98c\" rel=\"noopener\">multi-threading<\/a> will make you a quick learner. <\/em><em>In Python, there\u2019s a cool module called\u00a0<code class=\"db aeo aep aeq aer b\">threading.<\/code>\u00a0It\u2019s like a teamwork kit for your code, making things work together smoothly.<\/em><\/p>\n<h3 id=\"f92b\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\"><strong class=\"adb ng\">Let&#8217;s talk about taking turns first<\/strong><\/h3>\n<p id=\"f595\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\">The threading module in Python has four special classes that help your threads play fair and take turns. It\u2019s like having rules to ensure everyone gets a chance to do their job without any chaos.<\/p>\n<p id=\"7b82\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong class=\"adb ng\">Lock<\/strong><\/p>\n<p id=\"84be\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\">It is like a key that threads use to access a shared resource one at a time. It prevents conflicts by allowing only one Thread to hold the lock, ensuring the orderly execution of critical code sections.<\/p>\n<p data-selectable-paragraph=\"\"><em>Let\u2019s say Gautam is using a knife to chop something so Badal has to wait till Gautam releases the knife.<\/em><\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-61355 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-51-22.png\" alt=\"\" width=\"1832\" height=\"916\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-51-22.png 1832w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-51-22-300x150.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-51-22-1024x512.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-51-22-768x384.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-51-22-1536x768.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-51-22-624x312.png 624w\" sizes=\"(max-width: 1832px) 100vw, 1832px\" \/><\/p>\n<p data-selectable-paragraph=\"\"><em>\u201c<strong>Lock<\/strong>\u201d allows only one thread to acquire the lock at a time.<\/em><\/p>\n<p id=\"81db\" class=\"acz ada aej adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><em>If a thread holds the lock and tries to acquire it again (nested acquisition), it will be deadlocked. In other words, it doesn\u2019t allow the same thread to acquire the lock multiple times. <\/em><em>It\u2019s simple and suitable for basic synchronization needs.<\/em><\/p>\n<p id=\"1126\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong class=\"adb ng\">RLock (Re-entrant\/Recursive Lock)<\/strong><\/p>\n<p id=\"49a3\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\">It is much like\u00a0<code class=\"db aeo aep aeq aer b\">Lock<\/code>\u00a0and works like a key for thread to access a resource at a time. The only difference between the same\u00a0<code class=\"db aeo aep aeq aer b\">Lock<\/code>\u00a0and\u00a0<code class=\"db aeo aep aeq aer b\">RLock<\/code> is It can be acquired n times and released n times by the same thread, but to get it acquired by another thread the, the acquire and release count should be the same.<\/p>\n<p id=\"17a4\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\">The following code will create\u00a0<code class=\"db aeo aep aeq aer b\">DeadLock<\/code>\u00a0Since the same lock is getting acquired multiple times by the same thread.<\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-61351 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-41-25.png\" alt=\"Rlock Example\" width=\"1832\" height=\"916\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-41-25.png 1832w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-41-25-300x150.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-41-25-1024x512.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-41-25-768x384.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-41-25-1536x768.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-41-25-624x312.png 624w\" sizes=\"(max-width: 1832px) 100vw, 1832px\" \/><\/p>\n<p data-selectable-paragraph=\"\">To overcome this\u00a0<code class=\"db aeo aep aeq aer b\">DeadLock<\/code> the situation we can go for\u00a0<code class=\"db aeo aep aeq aer b\">RLock<\/code><\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-61352 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-43-17.png\" alt=\"Rlock Deadlock overcome\" width=\"1832\" height=\"916\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-43-17.png 1832w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-43-17-300x150.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-43-17-1024x512.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-43-17-768x384.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-43-17-1536x768.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-43-17-624x312.png 624w\" sizes=\"(max-width: 1832px) 100vw, 1832px\" \/><\/p>\n<p id=\"8e3a\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\">The\u00a0<code class=\"db aeo aep aeq aer b\">RLock<\/code>\u00a0will allow the same lock to be acquired by the same thread and\u00a0<code class=\"db aeo aep aeq aer b\">DeadLock<\/code>\u00a0the situation will be ignored.<\/p>\n<h3 id=\"4f63\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\"><strong class=\"adb ng\">Semaphore<\/strong><\/h3>\n<p id=\"dea1\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\">Semaphore is used to limit the number of threads that can access a resource at the same time. Works same as\u00a0<code class=\"db aeo aep aeq aer b\">Lock<\/code><\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-61353 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-46-09.png\" alt=\"Semaphore\" width=\"1832\" height=\"916\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-46-09.png 1832w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-46-09-300x150.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-46-09-1024x512.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-46-09-768x384.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-46-09-1536x768.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-from-2024-04-17-17-46-09-624x312.png 624w\" sizes=\"(max-width: 1832px) 100vw, 1832px\" \/><\/p>\n<p data-selectable-paragraph=\"\">\n<p id=\"a395\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong>Summary<\/strong><\/p>\n<p id=\"b927\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\">The Lock, RLock, and Semaphore classes enable you to make multiple threads operate on the same resource more efficiently. All three classes control the behavior of a thread over resources and can save you from inconsistency.<\/p>\n<p id=\"28c2\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong class=\"adb ng\">Official Website<\/strong>\u00a0<a class=\"af ol\" href=\"https:\/\/pythonbricks.pythonanywhere.com\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">https:\/\/pythonbricks.pythonanywhere.com\/<\/a><br \/>\n<strong class=\"adb ng\">Youtube Channel<\/strong>\u00a0<a class=\"af ol\" href=\"https:\/\/www.youtube.com\/@PythonBricks\" target=\"_blank\" rel=\"noopener ugc nofollow\">https:\/\/www.youtube.com\/@PythonBricks<\/a><br \/>\n<strong class=\"adb ng\">Telegram<\/strong>\u00a0<a class=\"af ol\" href=\"https:\/\/t.me\/pythonbricks\" target=\"_blank\" rel=\"noopener ugc nofollow\">https:\/\/t.me\/pythonbricks<\/a><\/p>\n<p id=\"de4d\" class=\"pw-post-body-paragraph acz ada yo adb b adc add ade adf adg adh adi adj nl adk adl adm np adn ado adp nt adq adr ads adt pv bj\" data-selectable-paragraph=\"\"><strong class=\"adb ng\">Watch and Learn: <\/strong><a class=\"af ol\" href=\"https:\/\/www.youtube.com\/watch?v=Ke8gBPq9ems\" target=\"_blank\" rel=\"noopener ugc nofollow\">https:\/\/www.youtube.chttps:\/\/www.youtube.com\/watch?v=Ke8gBPq9emsom<\/a><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Picture a kitchen in India with two cooks, Gautam and Badal. They\u2019re both preparing delicious dishes, but there\u2019s a unique spice box they both want to use. However, only one cook can use it at a time. Here\u2019s how they ensure a flavorful collaboration: Diverse Tasks: Gautam is making biryani, and Badal is crafting curry. [&hellip;]<\/p>\n","protected":false},"author":1695,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[1994,1],"tags":[5862,1559,5644,5645,3747,1358,5860,5861,5859,5647,5646],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61354"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1695"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=61354"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61354\/revisions"}],"predecessor-version":[{"id":61493,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61354\/revisions\/61493"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=61354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=61354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=61354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}