-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo1116_MultiThreading.cs
164 lines (152 loc) · 6.03 KB
/
No1116_MultiThreading.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace LeetCode_1116
{
class Program
{
// static void Main(string[] args)
// {
// var solution = new Solution();
// while (true)
// {
// //int input = int.Parse(Console.ReadLine());
// //int input2 = int.Parse(Console.ReadLine());
// //int input3 = int.Parse(Console.ReadLine());
// //string input = Console.ReadLine();
// //string input2 = Console.ReadLine();
// //int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
// //int input2 = int.Parse(Console.ReadLine());
// //var builder = new DataStructureBuilder();
// //int?[] data = new int?[] { 1, 2, 3, 4, 5, null, 6, null, null, 7, 8 };
// //var tree = builder.BuildTree(data);
// //var listNode = builder.BuildListNode(new int[] { 1, 4, 5 });
// //int[][] arr = new int[3][] { new int[] { 1, 3, 1 }, new int[] { 1, 5, 1 }, new int[] { 4, 2, 1 } };
// //string input = "abcbefga";
// //string input2 = "dbefga";
// //int[] nums2 = new int[] { 2, 1, 1, 5, 11, 5, 1, 7, 5, 6, 4, 3 };
// //int[] nums3 = new int[] { 10, 15, 20 };
// //int[] nums1 = new int[] { 10, 1, 2, 7, 6, 1, 5 };
// //IList<IList<int>> data = new List<IList<int>>()
// //{
// // new List<int>() { 1, 3 },
// // new List<int>() { 3, 0, 1 },
// // new List<int>() { 2 },
// // new List<int>() { 0 }
// // //new List<int>() { 1 },
// // //new List<int>() { 2 },
// // //new List<int>() { 3 },
// // //new List<int>() { }
// //};
// //var res = solution.MinimumOperations(input);
// //ConsoleX.WriteLine(res);
// }
// }
/// <summary>
/// 使用信号量来做,思路的解决办法比上一个 SpinWait 清晰很多。不过就是内存会多用些,可能在高频短时的情况下,不如SpinWait来的好
/// </summary>
public class ZeroEvenOdd
{
private int n;
private SemaphoreSlim _zero = new SemaphoreSlim(1, 1);
private SemaphoreSlim _odd = new SemaphoreSlim(0, 1);
private SemaphoreSlim _even = new SemaphoreSlim(0, 1);
public ZeroEvenOdd(int n)
{
this.n = n;
}
// printNumber(x) outputs "x", where x is an integer.
public void Zero(Action<int> printNumber)
{
for (int i = 1; i <= n; i++)
{
_zero.Wait();
printNumber(0);
if (i % 2 != 0)
_odd.Release();
else
_even.Release();
}
}
public void Even(Action<int> printNumber)
{
for (int i = 2; i <= n; i += 2)
{
_even.Wait();
printNumber(i);
_zero.Release();
}
}
public void Odd(Action<int> printNumber)
{
for (int i = 1; i <= n; i += 2)
{
_odd.Wait();
printNumber(i);
_zero.Release();
}
}
}
///// <summary>
///// 使用 SpinWait 解决,因为只是打印数字,所以用锁其实是一种浪费。(不过还是应该尝试用锁,毕竟是为了学习)
///// </summary>
//public class ZeroEvenOdd
//{
// private int n;
// //0 表示已经做过 zero 的打印了
// //1 表示已经做过 odd 的打印了
// //2 表示已经做过在 odd 之后的 zero 的打印了
// //3 表示已经做过 even 的打印了
// //初始的时候期望以零开始,所以是 3。
// private int _signal = 3;
// private int _count = 0;
// private SpinWait _spinWait = new SpinWait();
// public ZeroEvenOdd(int n)
// {
// this.n = n;
// }
// // printNumber(x) outputs "x", where x is an integer.
// public void Zero(Action<int> printNumber)
// {
// while (_count < n)
// {
// while (!(_signal == 1 || _signal == 3))
// {
// _spinWait.SpinOnce();
// if (_count >= n) return;
// }
// printNumber(0);
// _signal = (_signal + 1) % 4;
// }
// }
// public void Even(Action<int> printNumber)
// {
// while (_count < n)
// {
// while (_signal != 2)
// {
// _spinWait.SpinOnce();
// if (_count >= n) return;
// }
// printNumber(++_count);
// _signal = (_signal + 1) % 4;
// }
// }
// public void Odd(Action<int> printNumber)
// {
// while (_count < n)
// {
// if (_count >= n) return;
// while (_signal != 0)
// {
// _spinWait.SpinOnce();
// if (_count >= n) return;
// }
// printNumber(++_count);
// _signal = (_signal + 1) % 4;
// }
// }
//}
}
}