brutal/samples/Hello/HelloConcurrency.cs
Rian Drake 7a3b72d843
samples: add Hello/Demo samples for most BRUTAL packages (#19)
Add samples/, a collection of single-file .NET 10 apps run with
`dotnet run <file>.cs`. Hello/ has one minimal sample per package
that has a public API, many of them headless; Demo/ has fuller
programs that combine packages into a more interesting application.

samples/README.md indexes every sample and covers the layout, error
handling and per-OS requirements.
2026-10-27 09:10:25 +13:00

27 lines
801 B
C#

/*
* HelloConcurrency
*
* Runs a few jobs across worker threads with the JobScheduler and waits for them to finish.
*/
#:package Ahwoo.Brutal.Concurrency@2026.10.1
// RIAN: This job system is finely-tuned for the way RW projects like to implement concurrency, and has limitations we should document at some stage.
using Brutal.Concurrency.Jobs;
var scheduler = new JobScheduler(3, "HelloScheduler");
// The second argument is an estimated cost, used to balance jobs across worker threads.
for (var i = 1; i <= 6; i++)
scheduler.Queue(new HelloJob(i), 10);
scheduler.ExecuteJobs();
scheduler.Wait();
Console.WriteLine("All jobs finished");
class HelloJob(int id) : IJob
{
public void Run() => Console.WriteLine($"Job {id} running on thread {Environment.CurrentManagedThreadId}");
}