AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

XE 7 - Parallel Programming Library

Ein Thema von Alex_ITA01 · begonnen am 2. Sep 2014 · letzter Beitrag vom 3. Sep 2014
Antwort Antwort
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#1

AW: XE 7 - Parallel Programming Library

  Alt 3. Sep 2014, 14:54
Nur mal so zum Spass so ein einfaches Parallel
Delphi-Quellcode:
unit Parallel;

interface

uses
  System.SysUtils;

type
  TParallel = class
    class procedure Execute( ALow, AHigh : Int64; AProc : TProc<Int64> );
  end;

implementation

uses
  System.Generics.Collections,
  System.SyncObjs,
  System.Classes;

{ TParallel }

class procedure TParallel.Execute( ALow, AHigh : Int64; AProc : TProc<Int64> );
  procedure BuildThread( AEvent : TEvent; AValue : Int64 );
  var
    LThread : TThread;
  begin
    // Event zurücksetzen
    AEvent.ResetEvent;
    // Anonymen Thread erzeugen
    LThread := TThread.CreateAnonymousThread(
        procedure
      begin
        // Arbeit ausführen
        AProc( AValue );
        // Event setzen
        AEvent.SetEvent;
      end );
    // Thread starten
    LThread.Start;
  end;

var
  LMaxThreads : Integer;
  LThread : TThread;
  LIdx : Integer;
  LEvents : TList<TEvent>;
  LEventArray : THandleObjectArray;
  LSignaled : THandleObject;
  LEvent : TEvent;
  LIter : Int64;
begin

  // Events vorbereiten

  LMaxThreads := TThread.ProcessorCount;

  LEvents := TObjectList<TEvent>.Create( True );
  LEvents.Capacity := LMaxThreads;

  SetLength( LEventArray, LMaxThreads );
  for LIdx := low( LEventArray ) to high( LEventArray ) do
    begin
      LEvent := TEvent.Create( nil, True, True, '' );
      LEvents.Add( LEvent );
      LEventArray[LIdx] := LEvent;
    end;

  // Jetzt wird es parallel

  LIter := ALow;

  while LIter <= AHigh do
    begin
      // Warten, bis EIN Event wieder frei ist
      TEvent.WaitForMultiple( LEventArray, INFINITE, False, LSignaled );
      BuildThread( LSignaled as TEvent, LIter );
      Inc( LIter );
    end;
  // Warten, bis ALLE Events wieder frei sind
  TEvent.WaitForMultiple( LEventArray, INFINITE, True, LSignaled );
end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:32 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz