I have a time consuming routine which I\'d like to process in parallel using Delphi XE7\'s new parallel library.
Here is the single threaded version:
pro
The most obvious problem that I can see is that you queue to the worker thread.
Your call to TThread.Queue
passes TThread.CurrentThread
. That is the very thread on which you are calling TThread.Queue
. I think it is safe to say that you should never pass TThread.CurrentThread
to TThread.Queue
.
Instead, remove that parameter. Use the one parameter overload that just accepts a thread procedure.
Otherwise I'd note that the incrementing of the progress counter i
is not really handled correctly. Well, the incrementing is fine, but you then read it later and that's a race. You can report progress out of order if thread 1 increments before thread 2 but thread 2 queues progress before thread 1. Solve that by moving the counter increment code to the main thread. Simply increment it inside the queued anonymous method. Added bonus to that is you no longer need to use an atomic increment since all modifications are on the main thread.
Beyond that, this QC report seems rather similar to what you report: http://qc.embarcadero.com/wc/qcmain.aspx?d=128392
Finally, AtomicIncrement
is the idiomatic way to perform lock free incrementing in the latest versions of Delphi.