-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1117.cs
47 lines (41 loc) · 1.04 KB
/
Solution1117.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1117
{
/// <summary>
/// 1117. Building H2O - Medium
/// <a href="https://leetcode.com/problems/building-h2o">See the problem</a>
/// </summary>
public class H2O
{
private readonly object _lock = new();
private int _hCount = 0;
public void Hydrogen(Action releaseHydrogen)
{
lock (_lock)
{
while (_hCount == 2)
{
Monitor.Wait(_lock);
}
releaseHydrogen();
_hCount++;
Monitor.PulseAll(_lock);
}
}
public void Oxygen(Action releaseOxygen)
{
lock (_lock)
{
while (_hCount != 2)
{
Monitor.Wait(_lock);
}
releaseOxygen();
_hCount = 0;
Monitor.PulseAll(_lock);
}
}
}
}