# Classic C# Threads for [DragonECS](https://github.com/DCFApixels/DragonECS)
Support for processing entities in multiple threads, based on classic C# threads implementation. Inspired by a similar extension for [LeoEcs Lite](https://github.com/Leopotam/ecslite-threads).
> [!WARNING]
> The project is a work in progress, API may change.
>
> The most current version of the README is in [Russian version](https://github.com/DCFApixels/DragonECS-ClassicThreads/blob/main/README-RU.md).
# Installation
Versioning semantics - [Open](https://gist.github.com/DCFApixels/e53281d4628b19fe5278f3e77a7da9e8#file-dcfapixels_versioning_ru-md)
## Environment
Requirements:
+ Dependency: [DragonECS](https://github.com/DCFApixels/DragonECS)
+ Minimum version of C# 7.3;
Optional:
+ Support for NativeAOT
+ Game engines with C#: Unity, Godot, MonoGame, etc.
Tested with:
+ **Unity:** Minimum version 2020.1.0;
## Unity Installation
* ### Unity Package
The package can be installed as a Unity package by adding the Git URL [in the PackageManager](https://docs.unity3d.com/2023.2/Documentation/Manual/upm-ui-giturl.html) or manually adding it to `Packages/manifest.json`:
```
https://github.com/DCFApixels/DragonECS-ClassicThreads.git
```
* ### Source Code
The package can also be added to the project as source code.
# Parallel iteration
``` csharp
EcsThreadHandler _handler;
public void Run(EcsPipeline pipeline)
{
// Getting the Aspect and entities for iteration.
var group = _world.Where(out Aspect a);
void Handler(ReadOnlySpan entities)
{
foreach (var e in entities)
{
// Computations in a separate thread.
a.poses.Get(e).position += a.velocities.Read(e).value * _time.DeltaTime;
}
}
// Starts parallel iteration over entities,
// entities will be split into parts with a minimum size of 1000.
group.IterateParallel(_handler ??= Handler, 1000);
}
```
> The smaller the minimum size of the group part when dividing, the more threads can be utilized. In some situations, too many threads can negatively impact performance.
> Inside the handler, creating/deleting entities, adding/removing components on entities is prohibited. Only modification of data within components is allowed.