Capstone C: the job model

The third capstone is a pool of workers over a queue of jobs. It is the shape of a batch job, a thumbnail generator, a test runner and a web crawler: a producer that knows the work, a channel that holds it, workers that take one item at a time, and a summary at the end. This lesson writes the two types and runs the pool of one worker, which is the version without any concurrency at all, so the later lessons have something to compare against.

The job and the result

A job has an id, a number of units of work and a delay in milliseconds. The delay stands for the slow part of a real job, a disk read or an HTTP call, and it is what makes concurrency worth anything: work that is pure CPU and nothing else gets little from more workers than the machine has cores. A result has the id again, the value the work produced and whether the job succeeded, which is the shape lesson 129 needs when a worker fails.

Console.WriteLine("jobs 1.0"); var jobs = new[] { new Job(1, 1000, 20), new Job(2, 2500, 10), new Job(3, 500, 40), }; var total = 0L; foreach (var job in jobs) { var result = Work(job); total += result.Sum; Console.WriteLine($"job {result.Id} sum {result.Sum} ok {result.Ok}"); } Console.WriteLine($"jobs {jobs.Length} total {total}"); static JobResult Work(Job job) { Thread.Sleep(job.DelayMs); var sum = 0L; for (var i = 1; i <= job.Units; i++) { sum += i; } return new JobResult(job.Id, sum, job.Units > 0); } record Job(int Id, int Units, int DelayMs); record JobResult(int Id, long Sum, bool Ok);

dotnet run
echo "status: $?"

jobs 1.0
job 1 sum 500500 ok True
job 2 sum 3126250 ok True
job 3 sum 125250 ok True
jobs 3 total 3752000
status: 0

The work

Work sleeps for the delay of the job and then sums the integers from one to the number of units. The sum is the useful part: it is deterministic, so every run of the program prints the same four lines, and it is cheap enough that the sleep dominates. A benchmark over this program therefore measures the pool, not the arithmetic.

One at a time

The loop runs the three jobs one after another, and the program prints the results in the order of the jobs. The delays are twenty, ten and forty milliseconds, so the run cannot be shorter than seventy milliseconds; with three workers it could be as short as forty. That difference is the subject of the benchmark lesson.

A sequential run is the reference implementation of the capstone. Every concurrent version must produce the same results, and the total line is what a test compares: when the pool of lesson 129 prints 3 752 000, the concurrency did not lose a job.

The next lesson puts the jobs behind a bounded channel, where a producer writes and a consumer reads, and the two of them meet at the capacity of the channel.

Exercises

  1. Add a fourth job with zero units and decide whether it succeeds.
  2. Add the delay of the job to the result and print it, so the sequential run can be compared with the pool of lesson 129.
  3. Make Work throw for a negative number of units and print the exception in the loop.
  4. Replace the sum with a hash of the id and the units and print the same checksum line.

Questions

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

  1. Why does a job have a delay?

    Answer

    It stands for the slow part of a real job, which is what makes concurrency useful

    Work over pure CPU gains little from more workers than cores.

  2. What does the sequential loop guarantee?

    Answer

    The results arrive in the order of the jobs

    One worker and a loop leave no room for a different order.

  3. What does this program print? Console.WriteLine(Enumerable.Range(1, 500).Sum());

    Answer

    125250

    That is the sum of the third job, which the measured line shows.

  4. What is the last line of the output a checksum of?

    Answer

    The three results and the count of jobs

    A concurrent rewrite must print the same total line.

  5. What does this program print? var jobs = new[] { 20, 10, 40 }; Console.WriteLine(jobs.Sum());

    Answer

    70

    The delay of the whole run cannot be shorter than the sum of the delays.

  6. Why is Thread.Sleep wrong for a pool of workers?

    Answer

    It blocks the thread, which could run another job meanwhile

    The pool of lesson 129 gives the thread back with Task.Delay.

  7. What does this program print? Console.WriteLine(new R(1, 500500)); record R(int Id, long Sum);

    Answer

    R { Id = 1, Sum = 500500 }

    A record prints its type and its members with their names.

  8. What is the sum of one to a thousand?

    Answer

    500500

    The measured first result line reads sum 500500.

  9. What does this program print? var total = 0L; foreach (var n in new[] { 500500L, 3126250L, 125250L }) { total += n; } Console.WriteLine(total);

    Answer

    3752000

    That is the total line of the measured run.

  10. What is the reference implementation of this capstone?

    Answer

    The sequential loop, whose results every concurrent version must match

    A pool is correct when it prints the same totals as the loop.

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