{"id":10936,"date":"2015-04-02T08:56:19","date_gmt":"2015-04-02T12:56:19","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10936"},"modified":"2015-04-02T08:56:19","modified_gmt":"2015-04-02T12:56:19","slug":"semaphores-are-surprisingly-versatile","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/04\/02\/semaphores-are-surprisingly-versatile\/","title":{"rendered":"Semaphores are Surprisingly Versatile"},"content":{"rendered":"<p><a href=\"http:\/\/preshing.com\/20150316\/semaphores-are-surprisingly-versatile\/\">Jeff Preshing<\/a> (via <a href=\"https:\/\/twitter.com\/jfroy\/status\/577587901560672257\">Jean-Francois Roy<\/a>):<\/p>\n<blockquote cite=\"http:\/\/preshing.com\/20150316\/semaphores-are-surprisingly-versatile\/\"><p>I used to think semaphores were strange and old-fashioned. They were invented by Edsger Dijkstra <a href=\"http:\/\/en.wikipedia.org\/wiki\/Semaphore_%28programming%29\">back in the early 1960s<\/a>, before anyone had done much multithreaded programming, or much programming at all, for that matter. I knew that a semaphore could keep track of available units of a resource, or function as a clunky kind of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Semaphore_%28programming%29#Semaphores_vs._mutexes\">mutex<\/a>, but that seemed to be about it.<\/p>\n<p>My opinion changed once I realized that, using only semaphores and atomic operations, it&rsquo;s possible to implement all of the following primitives[&#8230;]<\/p>\n<p>Not only that, but these implementations share some desirable properties. They&rsquo;re <em>lightweight<\/em>, in the sense that some operations happen entirely in userspace, and they can (optionally) spin for a short period before sleeping in the kernel.<\/p>\n<p>[&#8230;]<\/p>\n<p>With all of these applications, semaphores are more general-purpose than I originally thought &#8211; and this wasn&rsquo;t even a complete list. So why are semaphores absent from the standard C++11 library? For the same reason they&rsquo;re absent from Boost: a preference for <a href=\"http:\/\/en.wikipedia.org\/wiki\/Monitor_%28synchronization%29\">mutexes and condition variables<\/a>. From the library maintainers&rsquo; point of view, conventional semaphore techniques are just <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2006\/n2043.html#SemaphoreTypes\">too error prone<\/a>.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Jeff Preshing (via Jean-Francois Roy): I used to think semaphores were strange and old-fashioned. They were invented by Edsger Dijkstra back in the early 1960s, before anyone had done much multithreaded programming, or much programming at all, for that matter. I knew that a semaphore could keep track of available units of a resource, or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[326,800,31,30,74,71],"class_list":["post-10936","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c-plus-plus","tag-concurrency","tag-ios","tag-mac","tag-opensource","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=10936"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10936\/revisions"}],"predecessor-version":[{"id":10937,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10936\/revisions\/10937"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}