Difference between revisions of "Micro-threading"

From Free Pascal wiki
Jump to navigationJump to search
(New page: ==Introduction== There are some situations where applications needs to execute a lots of asynchronous concurrent operations. OS-level threads could be used for this purpose but applicatio...)
 
(6 intermediate revisions by 3 users not shown)
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 multicore CPU and for writing more readable code. OS-level threads should be used 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.
There are some situations where applications needs to execute a lots of asynchronous concurrent operations. OS-level threads could be used for this purpose but application can create only limited count of threads. In matter of CPU utilization there are no significant performance gain if more threads is created then physical cores CPU have. In fact threads could be used for parallel processing on multicore CPU and for writing more readable code. OS-level thread should be used mainly used for parallelism and better CPU utilization. If programmer needs to write code which will be called asynchronously than OS-level threads are rather expensive in perspective of system resource.
 
 
 
  
 
===CPU context switching methods===
 
===CPU context switching methods===
 
+
* Cooperative (use of explicit [[Yield]] call)
* Cooperative (use of explicit Yield call)
 
 
* Preemptive (periodic timer based)
 
* Preemptive (periodic timer based)
 
* Combined
 
* Combined
  
 
===Objectives===
 
===Objectives===
 
 
* Unlimited number of instances (limited by available memory)
 
* Unlimited number of instances (limited by available memory)
 
* Fast switching, creation, destruction
 
* Fast switching, creation, destruction
 
* Automatic thread pool management by physical CPU core count
 
* Automatic thread pool management by physical CPU core count
* Ability to run in main loop only (without TThread instances)
+
* Ability to run in main loop only (without [[TThread]] instances)
 
* Provide own synchronization tools (Yield, Sleep, CriticalSection, Semaphore, Mutex, WaitForMultipleObjects, Queues, Synchronize, ...)
 
* Provide own synchronization tools (Yield, Sleep, CriticalSection, Semaphore, Mutex, WaitForMultipleObjects, Queues, Synchronize, ...)
 
* Priority control
 
* Priority control
 
* Support for view list of all microthreads
 
* Support for view list of all microthreads
  
 +
====Supported platforms====
  
 +
* Supported platforms are 32-bit Windows and Linux
  
 
==Implementation==
 
==Implementation==
 
+
* [http://svn.zdechov.net/PascalClassLibrary/MicroThreading/ MicroThreading] - Lazarus package, functional yet not finished, not multi-platform, need patching FPC
 
 
  
 
==External links==
 
==External links==
 
 
* [http://santonov.blogspot.com/2007/10/yield-you.html C# Yield implementation in Delphi]
 
* [http://santonov.blogspot.com/2007/10/yield-you.html C# Yield implementation in Delphi]
 
* [http://www.festra.com/wwwboard/messages/12899.html Cool little Coroutines function (Much better version)]
 
* [http://www.festra.com/wwwboard/messages/12899.html Cool little Coroutines function (Much better version)]
 
* [http://msdn.microsoft.com/en-us/library/ms682661%28v=vs.85%29.aspx Fibers(Windows)]  
 
* [http://msdn.microsoft.com/en-us/library/ms682661%28v=vs.85%29.aspx Fibers(Windows)]  
 +
* [https://code.google.com/p/meaop/source/browse/trunk/MeObjects/src/uMeCoroutine.pas MeSDK Delphi coroutine implementation]
  
 
+
[[Category:Parallel programming]]
 
[[Category:Multitasking]]
 
[[Category:Multitasking]]

Revision as of 05:31, 27 March 2020

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 multicore CPU and for writing more readable code. OS-level threads should be used 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

External links