{"id":2351,"date":"2017-01-16T00:13:47","date_gmt":"2017-01-15T22:13:47","guid":{"rendered":"http:\/\/mariusbancila.ro\/blog\/?p=2351"},"modified":"2017-01-20T12:13:13","modified_gmt":"2017-01-20T10:13:13","slug":"dining-philosophers-in-cpp11","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/","title":{"rendered":"Dining Philosophers in C++11"},"content":{"rendered":"<p><em><strong>UPDATE<\/strong>: for an implementation of the Chandy\/Misra solution see <a href=\"http:\/\/mariusbancila.ro\/blog\/2017\/01\/20\/dining-philosophers-in-c11-chandy-misra-algorithm\/\">Dining philosophers in C++11: Chandy-Misra algorithm<\/a><\/em><\/p>\n<p>The problem of the dining philosophers, first proposed by Edsger Dijkstra and reformulated by Tony Hoare, is a famous problem for concurrent programming that illustrates problems with synchronizing access to data. The description of the problem, taken from <a href=\"https:\/\/en.wikipedia.org\/wiki\/Dining_philosophers_problem\">Wikipedia<\/a>, is the following:<\/p>\n<blockquote><p>Five silent philosophers sit at a round table with bowls of spaghetti. Forks are placed between each pair of adjacent philosophers.<\/p>\n<p>Each philosopher must alternately think and eat. However, a philosopher can only eat spaghetti when they have both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it is not being used by another philosopher. After an individual philosopher finishes eating, they need to put down both forks so that the forks become available to others. A philosopher can take the fork on their right or the one on their left as they become available, but cannot start eating before getting both forks.<\/p>\n<p>Eating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply and an infinite demand are assumed.<\/p><\/blockquote>\n<p>The idea is to find a solution so that none of the philosophers would starve, i.e. never have the chance to acquire the forks necessary for him to eat.<\/p>\n<p>Below I propose a simple implementation to this problem using C++11 language and library features. The following classes are defined:<\/p>\n<ul>\n<li><tt>fork<\/tt> represents a fork at the table; the only member of this structure is a <tt>std::mutex<\/tt> that will be locked when the philosopher picks up the fork and unlocked when he puts it down.\n<pre class=\"lang:default decode:true \" >struct fork\r\n{\r\n   std::mutex mutex;\r\n};<\/pre>\n<\/li>\n<li><tt>table<\/tt> represents the round table where the philosophers are dining. It has an array of forks, but also an atomic boolean that indicates that the table is ready for the philosophers to start thinking and eating.\n<pre class=\"lang:default decode:true \" >struct table\r\n{\r\n   std::atomic&lt;bool&gt;                    ready{ false };\r\n   std::array&lt;fork, no_of_philosophers&gt; forks;\r\n};\r\n<\/pre>\n<\/li>\n<li><tt>philosopher<\/tt> represents a philosopher dining at the table. It has a name and a reference to the forks on his left and right.\n<pre class=\"lang:default decode:true \" >struct philosopher\r\n{\r\nprivate:\r\n   std::string const name;\r\n   table const &amp;     dinnertable;\r\n   fork&amp;             left_fork;\r\n   fork&amp;             right_fork;\r\n   std::thread       lifethread;\r\n   std::mt19937      rng{ std::random_device{}() };\r\n};<\/pre>\n<\/li>\n<\/ul>\n<p>Most of the implementation of the solution is part of the <tt>philosopher<\/tt> class. When an object of this class is instantiated, a thread is started. This thread is joined when the object is destroyed. The thread runs a loop of thinking and eating until the dinner is signaled to end by setting the <tt>ready<\/tt> member of the table to false. There are three main methods in the <tt>philosopher<\/tt> class:<\/p>\n<ul>\n<li><tt>dine()<\/tt> is the thread function; this is implemented as a simple loop of thinking and eating.\n<pre class=\"lang:default decode:true \" >   void dine()\r\n   {\r\n      while (!dinnertable.ready);\r\n\r\n      do\r\n      {\r\n         think();\r\n         eat();\r\n      } while (dinnertable.ready);\r\n   }<\/pre>\n<\/li>\n<li><tt>think()<\/tt> is the method that represents the thinking period. To model this the thread sleeps for a random period of time.\n<pre class=\"lang:default decode:true \" >   void think()\r\n   {\r\n      static thread_local std::uniform_int_distribution&lt;&gt; wait(1, 6);\r\n      std::this_thread::sleep_for(std::chrono::milliseconds(wait(rng) * 150));\r\n\r\n      print(\" is thinking \");\r\n   }<\/pre>\n<\/li>\n<li><tt>eat()<\/tt> is the method that models the eating. The left and right forks are acquired in a deadlock free manner using <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/thread\/lock\">std::lock<\/a>. After the forks, i.e. mutexes, are acquired, their ownership is transfered to a <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/thread\/lock_guard\">std::lock_guard<\/a> object, so that the mutexes are correctly released when the function returns. Eating is simulated with a sleep.\n<pre class=\"lang:default decode:true \" >   void eat()\r\n   {\r\n      std::lock(left_fork.mutex, right_fork.mutex);\r\n\r\n      std::lock_guard&lt;std::mutex&gt; left_lock(left_fork.mutex,   std::adopt_lock);\r\n      std::lock_guard&lt;std::mutex&gt; right_lock(right_fork.mutex, std::adopt_lock);\r\n\r\n      print(\" started eating.\");\r\n\r\n      static thread_local std::uniform_int_distribution&lt;&gt; dist(1, 6);\r\n      std::this_thread::sleep_for(std::chrono::milliseconds(dist(rng) * 50));\r\n\r\n      print(\" finished eating.\");\r\n   }<\/pre>\n<\/li>\n<\/ul>\n<p>To see how this works, we create a <tt>table<\/tt> object and an array of <tt>phylosopher<\/tt>s. Upon creating the <tt>philosopher<\/tt> objects their own working thread is started, but nothing happens until the table is signaled as being ready. Philosophers then compete for the forks (i.e. mutexes), eat and think until the dinner is signaled as finished by setting the ready flag of the <tt>table<\/tt> object back to false.<\/p>\n<p>The whole implementation is show below:<\/p>\n<pre class=\"lang:default decode:true \" >#include &lt;array&gt;\r\n#include &lt;mutex&gt;\r\n#include &lt;thread&gt;\r\n#include &lt;atomic&gt;\r\n#include &lt;chrono&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;random&gt;\r\n#include &lt;iomanip&gt;\r\n#include &lt;string_view&gt;\r\n\r\nstd::mutex g_lockprint;\r\nconstexpr  int no_of_philosophers = 5;\r\n\r\nstruct fork\r\n{\r\n   std::mutex mutex;\r\n};\r\n\r\nstruct table\r\n{\r\n   std::atomic&lt;bool&gt;                    ready{ false };\r\n   std::array&lt;fork, no_of_philosophers&gt; forks;\r\n};\r\n\r\nstruct philosopher\r\n{\r\nprivate:\r\n   std::string const name;\r\n   table const &amp;     dinnertable;\r\n   fork&amp;             left_fork;\r\n   fork&amp;             right_fork;\r\n   std::thread       lifethread;\r\n   std::mt19937      rng{ std::random_device{}() };\r\npublic:\r\n   philosopher(std::string_view n, table const &amp; t, fork &amp; l, fork &amp; r) :\r\n      name(n), dinnertable(t), left_fork(l), right_fork(r), lifethread(&amp;philosopher::dine, this)\r\n   {\r\n   }\r\n\r\n   ~philosopher()\r\n   {\r\n      lifethread.join();\r\n   }\r\n\r\n   void dine()\r\n   {\r\n      while (!dinnertable.ready);\r\n\r\n      do\r\n      {\r\n         think();\r\n         eat();\r\n      } while (dinnertable.ready);\r\n   }\r\n\r\n   void print(std::string_view text)\r\n   {\r\n      std::lock_guard&lt;std::mutex&gt; cout_lock(g_lockprint);\r\n      std::cout\r\n         &lt;&lt; std::left &lt;&lt; std::setw(10) &lt;&lt; std::setfill(' ')\r\n         &lt;&lt; name &lt;&lt; text &lt;&lt; std::endl;\r\n   }\r\n\r\n   void eat()\r\n   {\r\n      std::lock(left_fork.mutex, right_fork.mutex);\r\n\r\n      std::lock_guard&lt;std::mutex&gt; left_lock(left_fork.mutex,   std::adopt_lock);\r\n      std::lock_guard&lt;std::mutex&gt; right_lock(right_fork.mutex, std::adopt_lock);\r\n\r\n      print(\" started eating.\");\r\n\r\n      static thread_local std::uniform_int_distribution&lt;&gt; dist(1, 6);\r\n      std::this_thread::sleep_for(std::chrono::milliseconds(dist(rng) * 50));\r\n\r\n      print(\" finished eating.\");\r\n   }\r\n\r\n   void think()\r\n   {\r\n      static thread_local std::uniform_int_distribution&lt;&gt; wait(1, 6);\r\n      std::this_thread::sleep_for(std::chrono::milliseconds(wait(rng) * 150));\r\n\r\n      print(\" is thinking \");\r\n   }\r\n};\r\n\r\nvoid dine()\r\n{\r\n   std::this_thread::sleep_for(std::chrono::seconds(1));\r\n   std::cout &lt;&lt; \"Dinner started!\" &lt;&lt; std::endl;\r\n\r\n   {\r\n      table table;\r\n      std::array&lt;philosopher, no_of_philosophers&gt; philosophers\r\n      {\r\n         {\r\n            { \"Aristotle\", table, table.forks[0], table.forks[1] },\r\n            { \"Platon\",    table, table.forks[1], table.forks[2] },\r\n            { \"Descartes\", table, table.forks[2], table.forks[3] },\r\n            { \"Kant\",      table, table.forks[3], table.forks[4] },\r\n            { \"Nietzsche\", table, table.forks[4], table.forks[0] },\r\n         }\r\n      };\r\n\r\n      table.ready = true;\r\n      std::this_thread::sleep_for(std::chrono::seconds(5));\r\n      table.ready = false;\r\n   }\r\n\r\n   std::cout &lt;&lt; \"Dinner done!\" &lt;&lt; std::endl;\r\n}\r\n\r\nint main()\r\n{   \r\n   dine();\r\n\r\n   return 0;\r\n}<\/pre>\n<p>The output for this program (that varies with each execution) has the following form:<\/p>\n<pre class=\"lang:default decode:true \" >\r\nDinner started!\r\nDescartes  is thinking\r\nDescartes  started eating.\r\nDescartes  finished eating.\r\nPlaton     is thinking\r\nPlaton     started eating.\r\nAristotle  is thinking\r\nPlaton     finished eating.\r\nAristotle  started eating.\r\nDescartes  is thinking\r\nDescartes  started eating.\r\nAristotle  finished eating.\r\nDescartes  finished eating.\r\nNietzsche  is thinking\r\nNietzsche  started eating.\r\nKant       is thinking\r\nNietzsche  finished eating.\r\nKant       started eating.\r\nAristotle  is thinking\r\nAristotle  started eating.\r\nAristotle  finished eating.\r\nPlaton     is thinking\r\nPlaton     started eating.\r\n...\r\nKant       is thinking\r\nKant       started eating.\r\nKant       finished eating.\r\nDinner done!<\/pre>\n<p>Though the problem is usually described in terms of five philosophers, any number of philoposhers can be present at the table (of course, at least two are necessary for the problem to make sense). Adding more philosophers does not require any changes in the implementation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: for an implementation of the Chandy\/Misra solution see Dining philosophers in C++11: Chandy-Misra algorithm The problem of the dining philosophers, first proposed by Edsger Dijkstra and reformulated by Tony Hoare, is a famous problem for concurrent programming that illustrates problems with synchronizing access to data. The description of the problem, taken from Wikipedia, is &#8230; <a title=\"Dining Philosophers in C++11\" class=\"read-more\" href=\"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/\" aria-label=\"Read more about Dining Philosophers in C++11\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7],"tags":[451,345,502,504,190,503,346],"class_list":["post-2351","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-c11","tag-mutex","tag-philosophers","tag-problem","tag-synchronization","tag-thread"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"A C++11 solution to the famous problem of dining philosophers.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"c++,c++11,thread,mutex,synchronization,problem,philosophers\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Dining Philosophers in C++11\" \/>\n\t\t<meta property=\"og:description\" content=\"A C++11 solution to the famous problem of dining philosophers.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2017-01-15T22:13:47+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2017-01-20T10:13:13+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Dining Philosophers in C++11\" \/>\n\t\t<meta name=\"twitter:description\" content=\"A C++11 solution to the famous problem of dining philosophers.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/#article\",\"name\":\"Dining Philosophers in C++11\",\"headline\":\"Dining Philosophers in C++11\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"datePublished\":\"2017-01-16T00:13:47+02:00\",\"dateModified\":\"2017-01-20T12:13:13+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":9,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/#webpage\"},\"articleSection\":\"C++, C++, C++11, mutex, philosophers, problem, synchronization, thread\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/#listItem\",\"name\":\"Dining Philosophers in C++11\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/#listItem\",\"position\":5,\"name\":\"Dining Philosophers in C++11\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/\",\"name\":\"Dining Philosophers in C++11\",\"description\":\"A C++11 solution to the famous problem of dining philosophers.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/01\\\/16\\\/dining-philosophers-in-cpp11\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2017-01-16T00:13:47+02:00\",\"dateModified\":\"2017-01-20T12:13:13+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Dining Philosophers in C++11","description":"A C++11 solution to the famous problem of dining philosophers.","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/","robots":"max-image-preview:large","keywords":"c++,c++11,thread,mutex,synchronization,problem,philosophers","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/#article","name":"Dining Philosophers in C++11","headline":"Dining Philosophers in C++11","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"datePublished":"2017-01-16T00:13:47+02:00","dateModified":"2017-01-20T12:13:13+02:00","inLanguage":"en-US","commentCount":9,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/#webpage"},"articleSection":"C++, C++, C++11, mutex, philosophers, problem, synchronization, thread"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/#listItem","name":"Dining Philosophers in C++11"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/#listItem","position":5,"name":"Dining Philosophers in C++11","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/","name":"Dining Philosophers in C++11","description":"A C++11 solution to the famous problem of dining philosophers.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2017-01-16T00:13:47+02:00","dateModified":"2017-01-20T12:13:13+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"Dining Philosophers in C++11","og:description":"A C++11 solution to the famous problem of dining philosophers.","og:url":"https:\/\/mariusbancila.ro\/blog\/2017\/01\/16\/dining-philosophers-in-cpp11\/","article:published_time":"2017-01-15T22:13:47+00:00","article:modified_time":"2017-01-20T10:13:13+00:00","twitter:card":"summary","twitter:title":"Dining Philosophers in C++11","twitter:description":"A C++11 solution to the famous problem of dining philosophers."},"aioseo_meta_data":{"post_id":"2351","title":"Dining Philosophers in C++11","description":"A C++11 solution to the famous problem of dining philosophers.","keywords":[{"label":"C++","value":"C++"},{"label":"C++11","value":"C++11"},{"label":"thread","value":"thread"},{"label":"mutex","value":"mutex"},{"label":"synchronization","value":"synchronization"},{"label":"problem","value":"problem"},{"label":"philosophers","value":"philosophers"}],"keyphrases":null,"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":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"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":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"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":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-18 21:07:10","updated":"2025-12-12 07:45:21","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":9,"uagb_excerpt":"UPDATE: for an implementation of the Chandy\/Misra solution see Dining philosophers in C++11: Chandy-Misra algorithm The problem of the dining philosophers, first proposed by Edsger Dijkstra and reformulated by Tony Hoare, is a famous problem for concurrent programming that illustrates problems with synchronizing access to data. The description of the problem, taken from Wikipedia, is&hellip;","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++11<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">mutex<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">philosophers<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">problem<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">synchronization<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">thread<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">C++11<\/span>","<span class=\"advgb-post-tax-term\">mutex<\/span>","<span class=\"advgb-post-tax-term\">philosophers<\/span>","<span class=\"advgb-post-tax-term\">problem<\/span>","<span class=\"advgb-post-tax-term\">synchronization<\/span>","<span class=\"advgb-post-tax-term\">thread<\/span>"]}},"comment_count":"9","relative_dates":{"created":"Posted 10 years ago","modified":"Updated 10 years ago"},"absolute_dates":{"created":"Posted on January 16, 2017","modified":"Updated on January 20, 2017"},"absolute_dates_time":{"created":"Posted on January 16, 2017 12:13 am","modified":"Updated on January 20, 2017 12:13 pm"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-BV","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/2351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=2351"}],"version-history":[{"count":7,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/2351\/revisions"}],"predecessor-version":[{"id":2364,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/2351\/revisions\/2364"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=2351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=2351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=2351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}