1

I downloaded the OTL http://www.omnithreadlibrary.com/

and compile the D2007 grouproj, install the package, without problem.

I then create a simple console application that uses OtlParallel unit, of course, I add the OtlParallel and some other pas files to the project.

But it complains that Generics.Collections is not found.

René Hoffmann
  • 2,766
  • 2
  • 20
  • 43
justyy
  • 5,831
  • 4
  • 40
  • 73
  • OtlParallel is not supported in D2007 as D2007 doesn't have generic support. – gabr Oct 08 '15 at 11:51
  • 1
    that is what is said on the project home page " Currently, versions 2007, 2009, 2010, XE, XE2, XE3, XE4, XE5, XE6, XE7, XE8, and 10 Seattle are supported." – justyy Oct 08 '15 at 11:52
  • 1
    From http://otl.17slon.com/book/chap04.html#highlevel: "2.1 Introduction High-level abstractions are implemented in the OtlParallel unit. They are all created through the factory class Parallel. High-level code intensively uses anonymous methods and generics which makes Delphi 2009 the minimum supported version. As the implementation of generics in D2009 is not very stable, I’d recommend using at least Delphi 2010." – gabr Oct 08 '15 at 11:54
  • The download page includes snapshots of older versions. Have you tried any of them? – Rob Kennedy Oct 12 '15 at 15:36
  • I tried the earliest version (1.0.5a) that does not use Generics.Collections unit in OtlParallel, but it complains "reference to procedure" is undeclared. – justyy Oct 12 '15 at 16:11
  • "reference to procedure" is needed for support of anonymous methods. This is not possible in D2007. Try to comment out this construct. – LU RD Oct 12 '15 at 19:50

1 Answers1

2

The documentation says:

High-level abstractions are implemented in the OtlParallel unit. They are all created through the factory class Parallel. High-level code intensively uses anonymous methods and generics which makes Delphi 2009 the minimum supported version.

This us of both generics and anonymous methods makes this unit completely incompatible with Delphi 2007.

If you wish to use a construct like Parallel.For with Delphi 2007 and OTL then you will have to back-port OtlParallel yourself. Without anonymous methods this is very difficult to do and achieve the same fluid style of code. You would have to use procedural types instead of anonymous methods. And you would have to implement closures manually.

So instead of using

TProc = reference to procedure;

you would use

TMethod = procedure of object;

And then to implement this you create a class or record with a parameterless method. You'll need to add whatever state is needed as members of the type, and populate those members. That is in essence a manual implementation of a closure with variable capture. And you'll need to deal with lifetime. Make sure that the instances outlive the parallel loop.

Good luck!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • thanks.. I have implemented a simple ParallelFor and still figuring out why it has deadlocks http://stackoverflow.com/questions/33137336/delphi-asynccalls-deadlock-when-terminating-the-threads – justyy Oct 15 '15 at 09:07
  • 1
    That's not based on OTL though. – David Heffernan Oct 15 '15 at 09:11
  • I know. I implemented a ParallelFor based on AsyncCall using procedural call parameters, as you suggested. – justyy Oct 15 '15 at 09:21
  • In this answer I suggested basing it on OTL. OTL is supported on D2007, just not the high level interface. You could take OtlParallel and down-port it to D2007. – David Heffernan Oct 15 '15 at 09:22
  • Thanks.. actually, what do you meant by "Make sure that the instances outlive the parallel loop."? – justyy Oct 15 '15 at 09:23
  • 1
    For `Parallel.For` that's easy enough. You'll pass a method of a class or record. Then OTL will do what it does, and the methods will be called on different threads by some process of magic. The instances have to remain alive until their methods have finished executing. For `Parallel.For` that's simple because it is synchronous. It only returns when all iterations are complete, and then you can destroy these objects. For async approaches, it's harder. Anonymous methods have automatic lifetime management so this becomes a non-issue at least from the programmer's perspective. – David Heffernan Oct 15 '15 at 09:42
  • https://helloacm.com/simple-parallel-for-implementation-at-delphi-2007-without-generics-and-anonymous-methods/ – justyy Oct 16 '15 at 13:31