<?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>thread Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/thread/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/thread/</link>
	<description></description>
	<lastBuildDate>Thu, 25 Jul 2019 16:34:32 +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>thread Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/thread/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1117. Building H2O</title>
		<link>https://zxi.mytechroad.com/blog/concurrent/leetcode-1117-building-h2o/</link>
					<comments>https://zxi.mytechroad.com/blog/concurrent/leetcode-1117-building-h2o/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 25 Jul 2019 16:34:24 +0000</pubDate>
				<category><![CDATA[Concurrent]]></category>
		<category><![CDATA[concurrent]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[semaphore]]></category>
		<category><![CDATA[thread]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5354</guid>

					<description><![CDATA[<p>There are two kinds of threads,&#160;oxygen&#160;and&#160;hydrogen. Your goal is to group these threads to form water molecules.&#160;There is a barrier where each thread has to&#160;wait&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/concurrent/leetcode-1117-building-h2o/">花花酱 LeetCode 1117. Building H2O</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>There are two kinds of threads,&nbsp;<code>oxygen</code>&nbsp;and&nbsp;<code>hydrogen</code>. Your goal is to group these threads to form water molecules.&nbsp;There is a barrier where each thread has to&nbsp;wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given a&nbsp;<code>releaseHydrogen</code>&nbsp;and&nbsp;<code>releaseOxygen</code>&nbsp;method respectfully, which will allow them to pass the barrier. These threads should pass the barrier in groups of three, and they must be able to immediately bond with each other to form a water molecule.&nbsp;You must guarantee that all the threads from one molecule bond&nbsp;<em>before</em>&nbsp;any other threads from the next molecule do.</p>



<p>In other words:</p>



<ul><li>If an oxygen thread arrives at the barrier when no hydrogen threads are present, it has to wait for two hydrogen threads.</li><li>If a hydrogen thread arrives at the barrier when no other threads are present, it has to wait for an oxygen thread and another hydrogen thread.</li></ul>



<p>We don’t have to worry about matching the threads up explicitly; that is, the threads do not necessarily know which other threads they are paired up with. The key is just that threads pass the barrier in complete sets; thus, if we examine the sequence of threads that bond and divide them into groups of three, each group should contain one oxygen and two hydrogen threads.</p>



<p>Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.</p>



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



<pre class="wp-block-preformatted; crayon:false"><strong>Input: </strong>"HOH"
<strong>Output: </strong>"HHO"
<strong>Explanation:</strong> "HOH" and "OHH" are also valid answers.
</pre>



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



<pre class="wp-block-preformatted; crayon:false"><strong>Input: </strong>"OOHHHH"
<strong>Output: </strong>"HHOHHO"
<strong>Explanation:</strong> "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" and "OHHOHH" are also valid answers.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li>Total length of input string will be 3<em>n</em>, where 1 ≤&nbsp;<em>n</em>&nbsp;≤ 30.</li><li>Total number of&nbsp;<code>H</code>&nbsp;will be 2<em>n</em>&nbsp;in the input string.</li><li>Total number of&nbsp;<code>O</code>&nbsp;will&nbsp;be&nbsp;<em>n</em>&nbsp;in the input&nbsp;string.</li></ul>



<h2><strong>Solution: Semaphore</strong></h2>



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

<pre class="crayon-plain-tag">class Semaphore {
public:
    Semaphore(int s) : s_(s) {}

    void P(int d = 1) {
      unique_lock&lt;mutex&gt; lock(m_);
      while (s_ &lt; d) {
        cv_.wait(lock);
      }
      s_ -= d;
    }
  
    void V(int d = 1) {      
      unique_lock&lt;mutex&gt; lock(m_);
      s_ += d;      
      cv_.notify_all();
    }
private:
  mutex m_;
  condition_variable cv_;
  int s_;
};

class H2O {
public:
    H2O():s_h_(2), s_o_(2) {  }

    void hydrogen(function&lt;void()&gt; releaseHydrogen) {      
      s_h_.P();
      releaseHydrogen();
      s_o_.V();
    }

    void oxygen(function&lt;void()&gt; releaseOxygen) {
      s_o_.P(2);      
      releaseOxygen();
      s_h_.V(2);
    }
private:
  Semaphore s_h_;
  Semaphore s_o_;
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/concurrent/leetcode-1117-building-h2o/">花花酱 LeetCode 1117. Building H2O</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-1117-building-h2o/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
