Compute

API reference

Description

This service is used to create promises, which will run in separated threads - usefull for long running operations which completion should not block current thread, like heavy mathematical computing, synchronous I/O operations for which async API not exist etc.

Note:

Threads are not spawned each time you call schedule function - there is implemented small optimization which prevents unnessesary thread throttling.

Because of this optimization each phisical thread have 3 logical states: running, idle and readyToJoin. When you call schedule function, scheduler add your function to pending tasks list and check if there is thread in idle state. If there is at least one idle thread, there is no need to spawn new worker thread because one of idle threads will take pending task and execute it. If there is no idle thread, scheduler will spawn new thread, so it will take task to execute.

Thread which finished execution of current task will change state to idle and check if there is no pending tasks for some little amount of time, if there will be tasks to execute it will take one and change state to running. Otherwise it will go to ready readyToJoin state and will be destroyed soon by scheduler.

This way you can schedule with very small overhead even these tasks which can relatively fast complete - specially on platforms on which thread startup and teardown is not so fast.

This is something like variation about thread pool.