OmniThreadLibrary icon indicating copy to clipboard operation
OmniThreadLibrary copied to clipboard

IOmniFuture in dynamic array

Open Rura1980 opened this issue 6 years ago • 4 comments

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?

Rura1980 avatar Mar 15 '19 06:03 Rura1980

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

gabr42 avatar Mar 15 '19 08:03 gabr42

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.

Rura1980 avatar Mar 25 '19 13:03 Rura1980

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

gabr42 avatar Mar 25 '19 16:03 gabr42

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

Rura1980 avatar Mar 28 '19 07:03 Rura1980