Using the Delphi XE7 Parallel Library

前端 未结 1 1515
野的像风
野的像风 2021-02-09 23:44

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         


        
相关标签:
1条回答
  • 2021-02-10 00:12

    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.

    0 讨论(0)
提交回复
热议问题