{"id":5347,"date":"2019-07-23T09:22:32","date_gmt":"2019-07-23T16:22:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5347"},"modified":"2019-07-23T09:23:17","modified_gmt":"2019-07-23T16:23:17","slug":"leetcode-1115-print-foobar-alternately","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/concurrent\/leetcode-1115-print-foobar-alternately\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1115. Print FooBar Alternately"},"content":{"rendered":"\n<p>Suppose you are given the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\">class FooBar {\n  public void foo() {\n&nbsp; &nbsp; for (int i = 0; i &lt; n; i++) {\n&nbsp; &nbsp; &nbsp; print(\"foo\");\n&nbsp;   }\n  }\n\n  public void bar() {\n&nbsp; &nbsp; for (int i = 0; i &lt; n; i++) {\n&nbsp; &nbsp; &nbsp; print(\"bar\");\n&nbsp; &nbsp; }\n  }\n}\n<\/pre>\n\n\n\n<p>The same instance of&nbsp;<code>FooBar<\/code>&nbsp;will be passed to two different threads. Thread A will call&nbsp;<code>foo()<\/code>&nbsp;while thread B will call&nbsp;<code>bar()<\/code>.&nbsp;Modify the given program to output &#8220;foobar&#8221;&nbsp;<em>n<\/em>&nbsp;times.<\/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> n = 1\n<strong>Output:<\/strong> \"foobar\"\n<strong>Explanation:<\/strong> There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar(). \"foobar\" is being output 1 time.\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> n = 2\n<strong>Output:<\/strong> \"foobarfoobar\"\n<strong>Explanation:<\/strong> \"foobar\" is being output 2 times.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: 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++\">\nclass FooBar {\nprivate:\n  int n;\n  std::mutex m1_;\n  std::mutex m2_; \npublic:\n  FooBar(int n) {\n    this->n = n;    \n    m2_.lock();\n  }\n\n  void foo(function<void()> printFoo) {\n\n      for (int i = 0; i < n; i++) {\n        m1_.lock();\n        \/\/ printFoo() outputs \"foo\". Do not change or remove this line.\n        printFoo();\n        m2_.unlock();\n      }\n  }\n\n  void bar(function<void()> printBar) {\n\n      for (int i = 0; i < n; i++) {\n        m2_.lock();\n        \/\/ printBar() outputs \"bar\". Do not change or remove this line.\n        printBar();\n        m1_.unlock();\n      }\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you are given the following code: class FooBar { public void foo() { &nbsp; &nbsp; for (int i = 0; i &lt; n; i++)&#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,483,480],"class_list":["post-5347","post","type-post","status-publish","format-standard","hentry","category-concurrent","tag-concurrent","tag-easy","tag-mutex","tag-threading","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5347","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=5347"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5347\/revisions"}],"predecessor-version":[{"id":5350,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5347\/revisions\/5350"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}