### C# Job Scheduler Usage Example Source: https://github.com/genaray/zeroallocjobscheduler/blob/master/README.md Demonstrates the basic usage of the ZeroAllocJobScheduler in C#. This includes creating a scheduler instance with custom configuration, defining a job that implements the IJob interface, scheduling jobs, flushing the scheduler to execute jobs, and properly disposing of the scheduler. ```C# public class HeavyCalculation : IJob { public void Execute() { Thread.Sleep(50); // Simulate heavy work Console.WriteLine("Done"); } } // Create a new Scheduler, which you should keep the lifetime of your program. This is the only API call that will allocate or generate garbage. var scheduler = new JobScheduler(new JobScheduler.Config() { // Names the process "MyProgram0", "MyProgram1", etc. ThreadPrefixName = "MyProgram", // Automatically chooses threads based on your processor count ThreadCount = 0, // The amount of jobs that can exist in the queue at once without the scheduler spontaneously allocating and generating garbage. // Past this number, the scheduler is no longer Zero-Alloc! // Higher numbers slightly decrease performance and increase memory consumption, so keep this on the lowest possible end for your application. MaxExpectedConcurrentJobs = 64, // Enables or disables strict allocation mode: if more jobs are scheduled at once than MaxExpectedConcurrentJobs, it throws an error. // Not recommended for production code, but good for debugging allocation issues. StrictAllocationMode = false, }); // You need to pool/create jobs by yourself. This will, of course, allocate, so cache and reuse the jobs. var firstJob = new HeavyCalculation(); var firstHandle = scheduler.Schedule(firstJob); // Schedules job locally scheduler.Flush(); // Dispatches all scheduled jobs to the worker threads firstHandle.Complete(); // Blocks the thread until the job is complete. // Call Dispose at program exit, which shuts down all worker threads scheduler.Dispose(); ``` -------------------------------- ### C# Multiple Job Dependencies Source: https://github.com/genaray/zeroallocjobscheduler/blob/master/README.md Shows how to manage multiple dependencies for a single job in C# using ZeroAllocJobScheduler. It explains how to combine multiple job handles into a single dependency handle, allowing a job to start only after all its predecessors have finished. ```C# // You must create the array of handles, and handle caching/storage yourself. JobHandle[] handles = new JobHandle[2]; handles[0] = Scheduler.Schedule(job1); handles[1] = Scheduler.Schedule(job2); JobHandle combinedHandle = Scheduler.CombineDependencies(handles); // Combines all handles into the array into one var dependantHandle = Scheduler.Schedule(job3, combinedHandle); // job3 now depends on job1 and job2. // job1 and job2 can Complete() in parallel, but job3 can only run once both are complete. dependantHandle.Complete(); // Blocks the main thread until all three tasks are complete. ``` -------------------------------- ### C# Bulk Job Completion Source: https://github.com/genaray/zeroallocjobscheduler/blob/master/README.md Provides examples of how to efficiently wait for multiple jobs to complete in C# using ZeroAllocJobScheduler. It covers using `JobHandle.CompleteAll` with arrays and lists, as well as the alternative of calling `Complete()` on individual handles. ```C# JobHandle.CompleteAll(JobHandle[] handles); // Waits for all JobHandles to finish, and blocks the main thread until they each complete (in any order) JobHandle.CompleteAll(List handles); ``` -------------------------------- ### Define IJobParallelFor Job Source: https://github.com/genaray/zeroallocjobscheduler/blob/master/README.md Demonstrates how to define a job that extends `IJobParallelFor` for foreach-style indexing. Includes methods for `Execute` and `Finish`, and properties for `BatchSize` and `ThreadCount`. ```csharp public class ManyCalculations : IJobParallelFor { // Execute will be called for each i for the specified amount public void Execute(int i) { // ... do some operation with i here } // Finish will be called once all operations are completed. public void Finish() { Debug.Log("All done!"); } // BatchSize is a measure of how "complicated" your operations are. Detailed below. public int BatchSize => 32; // Restrict the number of spawned jobs to decrease memory usage and overhead. Keep this at 0 to use the Scheduler's number of active threads (recommended). public int ThreadCount => 0; } ``` -------------------------------- ### Schedule IJobParallelFor Job Source: https://github.com/genaray/zeroallocjobscheduler/blob/master/README.md Shows the syntax for scheduling an `IJobParallelFor` job with the scheduler. Specifies the job instance and the total number of iterations. ```csharp var job = new ManyCalculations(); var handle = scheduler.Schedule(job, 512); // Execute will be called 512 times ``` -------------------------------- ### C# Job Dependencies Source: https://github.com/genaray/zeroallocjobscheduler/blob/master/README.md Illustrates how to establish sequential dependencies between jobs using the ZeroAllocJobScheduler in C#. A job can be scheduled to execute only after a previously scheduled job has completed, ensuring a specific order of operations. ```C# var handle1 = scheduler.Schedule(job1); var handle2 = scheduler.Schedule(job2, handle1); // job2 will only begin execution once job1 is complete! scheduler.Flush(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.