<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lock Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/lock/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/lock/</link>
	<description></description>
	<lastBuildDate>Wed, 24 Jul 2019 10:45:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>lock Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/lock/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1116. Print Zero Even Odd</title>
		<link>https://zxi.mytechroad.com/blog/concurrent/leetcode-1116-print-zero-even-odd/</link>
					<comments>https://zxi.mytechroad.com/blog/concurrent/leetcode-1116-print-zero-even-odd/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 24 Jul 2019 10:45:27 +0000</pubDate>
				<category><![CDATA[Concurrent]]></category>
		<category><![CDATA[concurrent]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[mutex]]></category>
		<category><![CDATA[threading]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5351</guid>

					<description><![CDATA[<p>Suppose you are given the following code: class ZeroEvenOdd { &#160; public ZeroEvenOdd(int n) { ... }&#160; // constructor public void zero(printNumber) { ... }&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/concurrent/leetcode-1116-print-zero-even-odd/">花花酱 LeetCode 1116. Print Zero Even Odd</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Suppose you are given the following code:</p>



<pre class="wp-block-preformatted; crayon:false">class ZeroEvenOdd {
&nbsp; public ZeroEvenOdd(int n) { ... }&nbsp;     // constructor
  public void zero(printNumber) { ... }  // only output 0's
  public void even(printNumber) { ... }  // only output even numbers
  public void odd(printNumber) { ... }   // only output odd numbers
}
</pre>



<p>The same instance of&nbsp;<code>ZeroEvenOdd</code>&nbsp;will be passed to three different threads:</p>



<ol><li>Thread A will call&nbsp;<code>zero()</code>&nbsp;which should only output 0&#8217;s.</li><li>Thread B will call&nbsp;<code>even()</code>&nbsp;which should only ouput even numbers.</li><li>Thread C will call&nbsp;<code>odd()</code>&nbsp;which should only output odd numbers.</li></ol>



<p>Each of the thread is given a&nbsp;<code>printNumber</code>&nbsp;method to output&nbsp;an integer. Modify the given program to output the series&nbsp;<code>010203040506</code>&#8230; where the length of the series must be 2<em>n</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted; crayon:false"><strong>Input:</strong> n = 2
<strong>Output:</strong> "0102"
<strong>Explanation:</strong> There are three threads being fired asynchronously. One of them calls zero(), the other calls even(), and the last one calls odd(). "0102" is the correct output.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted; crayon:false"><strong>Input:</strong> n = 5
<strong>Output:</strong> "0102030405"</pre>



<h2><strong>Solution: Mutex</strong></h2>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 40 ms / 9 MB
class ZeroEvenOdd {
private:
  int x;
  int n;
  mutex m_zero_;
  mutex m_odd_;
  mutex m_even_;
public:
    ZeroEvenOdd(int n) {
      this-&gt;n = n;
      this-&gt;x = 1;
      m_odd_.lock();
      m_even_.lock();
    }

    // printNumber(x) outputs &quot;x&quot;, where x is an integer.
    void zero(function&lt;void(int)&gt; printNumber) {
      for (int i = 0; i &lt; n; ++i) {
        m_zero_.lock();
        printNumber(0);
        if (i % 2 == 0) {
          m_odd_.unlock();
        } else {
          m_even_.unlock();
        }
      }
    }

    void even(function&lt;void(int)&gt; printNumber) {
      for (int i = 2; i &lt;= n; i += 2) {
        m_even_.lock();
        printNumber(i);
        m_zero_.unlock();
      }
    }

    void odd(function&lt;void(int)&gt; printNumber) {
      for (int i = 1; i &lt;= n; i += 2) {
        m_odd_.lock();
        printNumber(i);
        m_zero_.unlock();      
      }
    }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/concurrent/leetcode-1116-print-zero-even-odd/">花花酱 LeetCode 1116. Print Zero Even Odd</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/concurrent/leetcode-1116-print-zero-even-odd/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
