gabr42 / OmniThreadLibrary

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

Assertion failure Parallel.ForEach( #168

Closed moctes closed 3 years ago

moctes commented 3 years ago

Hi,

I'm trying to do a small proof of concept sample based on the second example on the section 3.12.2.2 ... Enumerable collections of your book

The main difference: instead a TStringList I'm using a TObjectList.

The code ( typed here, I hope i didn't miss anything ) :

var opsInfo : TObjectList<TMyClass>; Begin opsInfo := nil; Try opsInfo := TObjectList<TMyClass>.Create(True); for var i := 1 to 10 do opsInfo.Add( TMyClass.Create(i) ); Parallel.ForEach(opsInfo) .NumTasks(4) .Execute( procedure (const value: TOmniValue) var anInstance: TMyClass; begin anInstance := value.AsObject as TMyClass; ProcessInstance( anInstance.Field1, anInstance.Field2); end); finally FreeAndNil(opsInfo); end; End; Executing that snippet, I get an assertion error on TOmniParallelLoopBase.Create here: FGetCurrent := rt.GetMethod('GetCurrent'); Assert(assigned(FGetCurrent));

FGetCurrent is nil, hence the error.

I'm testing on Delphi 10.3, I think I'm using OTL version 1.53b, I searched on the open/closed issues here, I'm hesitant to upgrade the OTL to the latest, but I'm open to suggestions.

Regards

Edit: The code looks ugly, I couldn't find how to preserve new lines

moctes commented 3 years ago

Never mind, I used IOmniBlockingCollection for the objects:

input := TOmniBlockingCollection.Create; ... populate input ... Parallel.ForEach(input)

and everything is working as expected.