Capstone C: logging without a race

A pool of workers that write to the console is a pool that shares one resource with no rule about who goes first. The rule this lesson uses is a stage: the workers write strings to a channel, and one task reads the channel and prints. The channel is the queue of log lines, and the task that reads it is the logger.

The logger as a stage

The first program is the smallest version: one producer writes eight lines and completes, the logger numbers each line with two digits and prints it, and the count is the return value of the logger task. With one producer the order of the lines is the order of the writes, so the output is the same on every run:

using System.Threading.Channels; Console.WriteLine("logging 1.0"); var log = Channel.CreateUnbounded<string>(); var writer = Task.Run(async () => { for (var i = 1; i <= 4; i++) { await log.Writer.WriteAsync($"job {i} started"); await log.Writer.WriteAsync($"job {i} finished"); } log.Writer.Complete(); }); var reader = Task.Run(async () => { var count = 0; await foreach (var line in log.Reader.ReadAllAsync()) { count++; Console.WriteLine($"{count:00} {line}"); } return count; }); await writer; var lines = await reader; Console.WriteLine($"lines {lines}");

dotnet run
echo "status: $?"

logging 1.0
01 job 1 started
02 job 1 finished
03 job 2 started
04 job 2 finished
05 job 3 started
06 job 3 finished
07 job 4 started
08 job 4 finished
lines 8
status: 0

The numbering is not decoration. A log line with a number says where it sits in the sequence the logger saw, and a gap in the numbers would be a lost line. The format {count:00} pads the count to two digits, which keeps the lines aligned up to ninety-nine.

Three workers, one log

The second program starts three workers, each of which writes two lines after a delay of its own. The logger is the same task as before: it reads what arrives and numbers it. The lines are whole, the numbers are dense from one to six, and the order of the workers is the order in which they reached the channel:

using System.Threading.Channels; Console.WriteLine("logger 1.0"); var log = Channel.CreateUnbounded<string>(); var reader = Task.Run(async () => { var count = 0; await foreach (var line in log.Reader.ReadAllAsync()) { count++; Console.WriteLine($"{count:00} {line}"); } return count; }); var workers = Enumerable.Range(1, 3).Select(worker => Task.Run(async () => { for (var step = 1; step <= 2; step++) { await Task.Delay(worker * 3); await log.Writer.WriteAsync($"worker {worker} step {step}"); } })).ToArray(); await Task.WhenAll(workers); log.Writer.Complete(); Console.WriteLine($"lines {await reader}");

dotnet run
echo "status: $?"

logger 1.0
01 worker 1 step 1
02 worker 3 step 1
03 worker 2 step 1
04 worker 1 step 2
05 worker 2 step 2
06 worker 3 step 2
lines 6
status: 0

In this run worker one came first, then three, then two; the delays are three, six and nine milliseconds and the order followed them. A second run can put the three in another order, and the numbers still go from one to six without a gap, because the numbering happens where the lines are read and not where they are written.

The race that remains

The logger removes the race over the console, and it does not remove the race over the order. Two lines written at the same moment by two workers have no promised order, and a channel does not invent one: it carries what was handed to it in the order it was handed over. What the logger does promise is that the order in the log is the order of arrival, that every line is complete, and that the count is the number of lines written.

The last lesson of the capstone measures the pool: the same eight jobs with one worker and with four, five runs each, and the minimum and the median of the time.

Exercises

  1. Add a level to every line (info, warn) and print it in a column of its own.
  2. Write the log to a file with a StreamWriter and read it back with File.ReadLines.
  3. Make the logger count the lines of each worker and print a summary at the end.
  4. Replace the unbounded channel with a bounded one of capacity two and explain what the workers wait for.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why is the logger a task of its own?

    Answer

    One reader of the console means the lines are whole and numbered in arrival order

    The workers write to a channel and one task writes the console.

  2. What does the number of a line tell you?

    Answer

    Its place in the sequence the logger read, and a gap would be a lost line

    The numbering happens at the read, so it is dense.

  3. What does this program print? using System.Threading.Channels; var channel = Channel.CreateUnbounded<string>(); await channel.Writer.WriteAsync("a"); await channel.Writer.WriteAsync("b"); channel.Writer.Complete(); var count = 0; await foreach (var line in channel.Reader.ReadAllAsync()) { count++; Console.WriteLine($"{count:00} {line}"); }

    Answer

    01 a and 02 b

    The count starts at zero and is incremented before it is printed.

  4. What is promised about the order of the six lines of the second program?

    Answer

    Only that it is the order of arrival at the channel

    The delays decide which worker reaches the channel first.

  5. What does this program print? Console.WriteLine($"{7:00} and {123:00}");

    Answer

    07 and 123

    The format pads to two characters and never truncates.

  6. Why can an entry of two Console.WriteLine calls be split by another thread?

    Answer

    The lock covers one call, not two

    That is why an entry is one call and one line.

  7. What does this program print? var lines = new[] { "worker 1", "worker 2" }; Console.WriteLine(lines.Length);

    Answer

    2

    Two lines make a length of two.

  8. What would a gap in the numbering mean?

    Answer

    A line was lost between the writer and the log

    The count is incremented once per line read.

  9. What does a bounded log channel change?

    Answer

    The workers wait when the logger falls behind

    A full channel gives the writer backpressure instead of memory growth.

  10. What is the shape of a logger in a pipeline?

    Answer

    Many writers, one channel, one reader that owns the sink

    The channel turns many producers into one consumer of the sink.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev