gabr42 / OmniThreadLibrary

A simple and powerful multithreading library for Delphi
http://www.omnithreadlibrary.com
Other
465 stars 140 forks source link

IOmniFuture in dynamic array #131

Open Rura1980 opened 5 years ago

Rura1980 commented 5 years ago

I call Parallel.Future in a for loop and store IOmniFuture in a array of IOmniFuture. Later I want to iterate through this array and call future.Value for each element. Unfortunately, this code fails occasionly with TList.IndexOutOfBounds. When I wait for the future to complete before adding to dynamic array, everything works fine. What am I doing wrong?

gabr42 commented 5 years ago

Provide a minimal working example (http://sscce.org), please.

Rura1980 commented 5 years ago

I am using Delphi XE. Modified code a little bit and now my program crashes (but not always) with access violation in OtlSync line 1891 FAwaitedLock.Acquire.

It seems to be something quite simple, but I cannot figure it out... Any comments are welcomed.

program DeCalTest;

{$APPTYPE CONSOLE}

uses FastMM4, Windows, SysUtils, Classes, SyncObjs, Generics.Collections, OtlParallel, OtlCommon, OtlCollections, OtlTask, OtlTaskControl;

procedure ProcessMessages; var Msg: TMsg; begin while PeekMessage( Msg, 0, 0, 0, PM_REMOVE ) do begin TranslateMessage( Msg ); DispatchMessage( Msg ); end; end;

procedure TestFutures; var x: array of IOmniFuture; j: integer; begin SetLength( x, 70 ); for j := 0 to Length( x ) - 1 do begin x[j] := Parallel.Future( function: integer begin TThread.CurrentThread.Sleep( 100 ); Result := 0; end ); end; ProcessMessages; for j := 0 to Length(x) - 1 do begin Writeln( Format( 'x[%d] = %d', [j, x[j].Value] ) ); x[j] := nil; end; SetLength(x, 0); x := nil; end;

begin ReportMemoryLeaksOnShutdown := DebugHook <> 0; Randomize;

TestFutures; Writeln( 'Press any key' ); ProcessMessages; Readln;

end.

This program crashed but not always with access violation in OtlSync line 1891 FAwaitedLock.Acquire.

gabr42 commented 5 years ago

Sorry, but I can't repeat your problem with Delphi 10.3 Rio and latest OmniThreadLibrary source code.

Rura1980 commented 5 years ago

I cannot repeat my problem with Delphi 10.3 Rio either. Very strange.