SanderMertens / flecs

A fast entity component system (ECS) for C & C++
https://www.flecs.dev
Other
6.46k stars 454 forks source link

Thread pinning #284

Open randy408 opened 3 years ago

randy408 commented 3 years ago

Describe the problem you are trying to solve. Pinning systems to threads is important to keep specific library calls on the main thread, prevent splitting of a module's systems across threads and to allow thread-safe systems to run on any thread at the same time.

Describe the solution you'd like

* Updated *

// Always run on thread idx 0 by default
ECS_SYSTEM("Move", "Position, Velocity");

//Mark system as thread-safe, alternative names: MultiThread, AllThreads, AnyThread
ECS_SYSTEM("X", "SYSTEM:ThreadSafe Lorem, Ipsum");

//Creates an entity
ECS_THREAD(MotionThread);

//"Thread" is a builtin trait, makes it more obvious `MotionThread` is a thread
//System is not split across threads, but the thread index may change
ECS_SYSTEM("Move", "SYSTEM:Thread FOR MotionThread, Position, Velocity");

//Also not split across threads and on the same thread as Move
ECS_SYSTEM("X", "SYSTEM:Thread FOR MotionThread Lorem, Ipsum");

//Alternative syntax
ECS_SYSTEM("Move", "THREAD:MotionThread Position, Velocity");

Why something like THREAD:0-N would be a bad idea: Modules from different sources could hog the same thread and assumes flecs will spawn N threads, named threads are also better for debugging.

alex-rass-88 commented 3 years ago

Have some question about threading. Can i create custom pipeline with some stages marked as single threaded? I need 2 stages what runs on main thread and 5 stages what multi threaded.