Coder Social home page Coder Social logo

sea1812 / hpthreadpool Goto Github PK

View Code? Open in Web Editor NEW

This project forked from renancostab/hpthreadpool

0.0 0.0 0.0 105 KB

High Performance Thread Pool - A simple and nice thread pool design for Lazarus

License: Mozilla Public License 2.0

Pascal 100.00%

hpthreadpool's Introduction

Version License Lang

HPThreadPool

HPThreadPool is a High Performance Thread Pool design for Lazarus Free pascal

Design

The goal is to provide a nice and simple way to work with threads without the excessive overhead of runtime thread creation, the major topics of a thread pool is covered, including:

  • Statics
    • Minimum
    • Maximum
    • Threads running
    • Work enqueued
  • Resize of thread pool
  • Zero sleep time when a job is available
  • WaitForAll and WaitForAny
  • Worker thread class customization (if needed)
  • OnTerminateWorker callback sync (Synchronized) and async (Thread context)

Quick Example

unit Demo;

interface

uses
 HPThreadPool;
 
type
 TMyJobs = class(TObject)
 private
 public
   class procedure MyFiboWork(AParameters: Pointer);
 end;
 
implementation

procedure TMyJobs.MyFiboWork(AParameters: Pointer);
var
 I: Integer;
 Value: Integer; 
 Sum: Integer = 0;
 NumA: Integer = 1;
 NumB: Integer = 0;
begin
 Value := PInteger(AParameters)^;
 
 for I := 1 to Value do
 begin
   Sum := NumA + NumB;
   NumB := NumA;
   NumA := Sum;
 end;
 
 PInteger(AParameters)^ := Sum;
end;

// Example working with 100 jobs and a random value of fibonnaci
const
 MAXJOBS = 100;
var
 I: Integer;
 Fibo: array of Integer;  
 Tasks: TTaskList;
begin
 Randomize;
 Tasks := TTaskList.Create;
 SetLength(Fibo, MAXJOBS);
 
 for I := 0 to MAXJOBS - 1 do
 begin
   Fibo[I] := Random(10000);
   Tasks.Add(THPTask.Run(@TMyJobs.MyFiboWork, @Fibo[I]));
 end;
 
 THPTask.WaitForAll(Tasks.ToArray);
 
 for I := 0 to High(Fibo) do
   WriteLn(I, ' - ', Fibo[I]);
 
 Tasks.Free;
end;

hpthreadpool's People

Contributors

renancostab avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.