{"id":5344,"date":"2019-07-23T08:58:56","date_gmt":"2019-07-23T15:58:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5344"},"modified":"2019-07-23T09:21:50","modified_gmt":"2019-07-23T16:21:50","slug":"leetcode-1114-print-in-order","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/concurrent\/leetcode-1114-print-in-order\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1114. Print in Order"},"content":{"rendered":"\n<p>Suppose we have a class:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Foo {\n&nbsp; public void first() { print(\"first\"); }\n&nbsp; public void second() { print(\"second\"); }\n&nbsp; public void third() { print(\"third\"); }\n}\n<\/pre>\n\n\n\n<p>The same instance of&nbsp;<code>Foo<\/code>&nbsp;will be passed to three different threads. Thread A will call&nbsp;<code>first()<\/code>, thread B will call&nbsp;<code>second()<\/code>, and thread C will call&nbsp;<code>third()<\/code>. Design a mechanism and modify the program&nbsp;to ensure that&nbsp;<code>second()<\/code>&nbsp;is executed after&nbsp;<code>first()<\/code>, and&nbsp;<code>third()<\/code>&nbsp;is executed after&nbsp;<code>second()<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [1,2,3]\n<strong>Output:<\/strong> \"firstsecondthird\"\n<strong>Explanation:<\/strong> There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). \"firstsecondthird\" is the correct output.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [1,3,2]\n<strong>Output:<\/strong> \"firstsecondthird\"\n<strong>Explanation:<\/strong> The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). \"firstsecondthird\" is the correct output.<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<p>We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seems to imply the ordering. The input format you see is mainly to ensure our tests&#8217; comprehensiveness.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Mutex<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 128 ms \/ 8.2 MB\nclass Foo {\npublic:\n    Foo() {\n      first_.lock();\n      second_.lock();\n    }\n\n    void first(function<void()> printFirst) {        \n      \/\/ printFirst() outputs \"first\". Do not change or remove this line.\n      printFirst();  \n      first_.unlock();\n    }\n\n    void second(function<void()> printSecond) {            \n      first_.lock();\n      \/\/ printSecond() outputs \"second\". Do not change or remove this line.\n      printSecond();\n      first_.unlock();\n      second_.unlock();\n    }\n\n    void third(function<void()> printThird) {\n      second_.lock();\n      \/\/ printThird() outputs \"third\". Do not change or remove this line.\n      printThird();      \n      second_.unlock();\n    }\nprivate:\n  std::mutex first_;\n  std::mutex second_;\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Busy waiting<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 176 ms \/ 9.2 MB\n#include <unistd.h>\n\nclass Foo {\npublic:\n    Foo() {}\n\n    void first(function<void()> printFirst) {        \n      \/\/ printFirst() outputs \"first\". Do not change or remove this line.\n      printFirst();  \n      state_ = 1;\n    }\n\n    void second(function<void()> printSecond) {            \n      while (state_ != 1) usleep(1);\n      \/\/ printSecond() outputs \"second\". Do not change or remove this line.\n      printSecond();\n      state_ = 2;\n    }\n\n    void third(function<void()> printThird) {\n      while (state_ != 2) usleep(1);\n      \/\/ printThird() outputs \"third\". Do not change or remove this line.\n      printThird();      \n      state_ = 3;\n    }\nprivate:\n  int state_ = 0;\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Suppose we have a class: public class Foo { &nbsp; public void first() { print(&#8220;first&#8221;); } &nbsp; public void second() { print(&#8220;second&#8221;); } &nbsp; public&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[478],"tags":[479,222,481,483,480],"class_list":["post-5344","post","type-post","status-publish","format-standard","hentry","category-concurrent","tag-concurrent","tag-easy","tag-multithreading","tag-mutex","tag-threading","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5344"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5344\/revisions"}],"predecessor-version":[{"id":5346,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5344\/revisions\/5346"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}