dougbinks / enkiTS

A permissively licensed C and C++ Task Scheduler for creating parallel programs. Requires C++11 support.
zlib License
1.66k stars 138 forks source link

The threads responsible for executing the tasks #121

Closed liamhauw closed 4 months ago

liamhauw commented 4 months ago

When I initialize enki::TaskScheduler as follows:

enki::TaskScheduler task_scheduler;
enki::TaskSchedulerConfig task_scheduler_config;
task_scheduler_config.numTaskThreadsToCreate = 3;
task_scheduler.Initialize(task_scheduler_config);

Does this mean that the task_scheduler is initialized with a thread number of 0, and the threads responsible for executing the tasks range from 1 to 3?

But why are some of the tasks I execute appearing on thread 0?

dougbinks commented 4 months ago

Hi!

Does this mean that the task_scheduler is initialized with a thread number of 0, and the threads responsible for executing the tasks range from 1 to 3?

Yes, this is correct.

But why are some of the tasks I execute appearing on thread 0?

Thread 0, the thread the task scheduler is initialized on, can use the enkiTS task API, and will run tasks under several circumstances:

  1. When AddTaskSetToPipe and the pipe is full, enkiTS will run a range of the taskset. See #58 for a discussion of this, along with how to avoid it by using a task to launch multiple tasks rather than doing so from the main thread.
  2. When a Waitfor* API call is made, enkiTS will run tasks whilst waiting. See the comments for more details.
  3. When RunPinnedTasks is called, the main thread will run any tasks which have been pinned to thread 0.

Does that help clarify the behaviour for you?

liamhauw commented 4 months ago

Thank! It's very helpful to me.