Difference between revisions of "Micro-threading"
From Lazarus wiki
Jump to navigationJump to searchm (→Implementation) |
|||
Line 1: | Line 1: | ||
==Introduction== | ==Introduction== | ||
− | There are some situations where applications need to execute lots of asynchronous concurrent operations. OS-level threads could be used for this purpose, but an application can create only a limited count of threads. As for CPU utilization there is no significant performance gain if more threads are created than physical cores available on a CPU. In fact threads could be used for parallel processing on a | + | There are some situations where applications need to execute lots of asynchronous concurrent operations. OS-level threads could be used for this purpose, but an application can create only a limited count of threads. As for CPU utilization there is no significant performance gain if more threads are created than physical cores available on a CPU. In fact threads could be used for parallel processing on a multi-core CPU and for writing more readable code. OS-level threads should be mainly used for parallelism and better CPU utilization. If a programmer needs to write code which will be called asynchronously, then OS-level threads will be rather expensive in perspective of system resources. |
===CPU context switching methods=== | ===CPU context switching methods=== |
Revision as of 11:56, 31 August 2021
Introduction
There are some situations where applications need to execute lots of asynchronous concurrent operations. OS-level threads could be used for this purpose, but an application can create only a limited count of threads. As for CPU utilization there is no significant performance gain if more threads are created than physical cores available on a CPU. In fact threads could be used for parallel processing on a multi-core CPU and for writing more readable code. OS-level threads should be mainly used for parallelism and better CPU utilization. If a programmer needs to write code which will be called asynchronously, then OS-level threads will be rather expensive in perspective of system resources.
CPU context switching methods
- Cooperative (use of explicit Yield call)
- Preemptive (periodic timer based)
- Combined
Objectives
- Unlimited number of instances (limited by available memory)
- Fast switching, creation, destruction
- Automatic thread pool management by physical CPU core count
- Ability to run in main loop only (without TThread instances)
- Provide own synchronization tools (Yield, Sleep, CriticalSection, Semaphore, Mutex, WaitForMultipleObjects, Queues, Synchronize, ...)
- Priority control
- Support for view list of all microthreads
Supported platforms
- Supported platforms are 32-bit Windows and Linux
Implementation
- MicroThreading - Lazarus package, functional yet not finished, not multi-platform, need patching FPC
- STAX - Single Threaded Asynchronous EXecution framework (STAX for short) enables async/await style co-routines for Free Pascal