Skip to content

Commit 4a61ce3

Browse files
committed
..
1 parent 313af86 commit 4a61ce3

File tree

3 files changed

+126
-6
lines changed

3 files changed

+126
-6
lines changed

cpp.boost/bounded.buffer/bounded.buffer.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ class BoundedBuffer
3030

3131
BoundedBuffer(const BoundedBuffer&) = delete;
3232

33-
int count()
34-
{
35-
return m_unread;
36-
}
33+
auto count() const { return m_unread; }
3734

3835
auto push_front(const value_type& item) -> bool
3936
{
@@ -167,7 +164,6 @@ void fifo_test(Buffer* buffer)
167164
for (int i=0; i<100; i++) buffer->push_front(i);
168165

169166
// 2. prepare producers
170-
171167
vector<thread> pool;
172168
Consumer<Buffer> consumer(buffer);
173169
thread consume(consumer);
@@ -181,7 +177,9 @@ void fifo_test(Buffer* buffer)
181177
buffer->shutdown();
182178

183179
// x. Joint the threads
184-
for (auto& t : pool) if (t.joinable()) t.join();
180+
for (auto& t : pool)
181+
if (t.joinable()) t.join();
182+
185183
consume.join();
186184
}
187185

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Runtime.InteropServices;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
class ProdoucerConsumer
8+
{
9+
private readonly int inputs = 2000;
10+
private CancellationTokenSource ct;
11+
private BlockingCollection<int> numbers;
12+
private int capacity;
13+
14+
public ProdoucerConsumer(int capa)
15+
{
16+
ct = new CancellationTokenSource();
17+
capacity = capa;
18+
numbers = new BlockingCollection<int>(capacity);
19+
}
20+
21+
public void Shutdown()
22+
{
23+
ct.Cancel();
24+
}
25+
26+
public void Run()
27+
{
28+
// Wait for the tasks to complete execution
29+
Task.WaitAll(Task.Run(() => Consumer()),
30+
Task.Run(() => Producer()));
31+
32+
ct.Dispose();
33+
Console.WriteLine("Press the Enter key to exit.");
34+
Console.ReadLine();
35+
}
36+
37+
void Consumer()
38+
{
39+
while (!numbers.IsCompleted)
40+
{
41+
int nextItem = 0;
42+
try
43+
{
44+
if (!numbers.TryTake(out nextItem, 0, ct.Token))
45+
Console.WriteLine(" Take Blocked");
46+
else
47+
Console.WriteLine(" Take:{0}", nextItem);
48+
}
49+
catch (OperationCanceledException)
50+
{
51+
Console.WriteLine("Taking canceled.");
52+
break;
53+
}
54+
55+
// REAMRK
56+
// : consumers work here!
57+
// Slow down consumer just a little to cause
58+
// collection to fill up faster, and lead to "AddBlocked"
59+
Thread.SpinWait(500000);
60+
}
61+
62+
Console.WriteLine("\r\nNo more items to take.");
63+
}
64+
65+
void Producer()
66+
{
67+
int itemToAdd = 0;
68+
bool success = false;
69+
70+
do
71+
{
72+
// Cancellation causes OCE. We know how to handle it.
73+
try
74+
{
75+
// A shorter timeout causes more failures.
76+
success = numbers.TryAdd(itemToAdd, 2, ct.Token);
77+
}
78+
catch (OperationCanceledException)
79+
{
80+
Console.WriteLine("Add loop canceled.");
81+
// Let other threads know we're done in case
82+
// they aren't monitoring the cancellation token.
83+
numbers.CompleteAdding();
84+
break;
85+
}
86+
87+
if (success)
88+
{
89+
Console.WriteLine(" Add:{0}", itemToAdd);
90+
itemToAdd++;
91+
}
92+
else
93+
{
94+
Console.Write(" AddBlocked:{0} Count = {1}", itemToAdd.ToString(), numbers.Count);
95+
// Don't increment nextItem. Try again on next iteration.
96+
97+
//Do something else useful instead.
98+
// UpdateProgress(itemToAdd);
99+
}
100+
} while (itemToAdd < inputs);
101+
102+
// No lock required here because only one producer.
103+
numbers.CompleteAdding();
104+
}
105+
}
106+
107+
class ProgramWithCancellation
108+
{
109+
static void Main()
110+
{
111+
var pc = new ProdoucerConsumer();
112+
pc.Run();
113+
}
114+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
</Project>

0 commit comments

Comments
 (0)