0

I have some code that I'm trying to parallelize with Delphi's 7 Parallel Library. The routine I would like to parallelize adds a TObject to a TStringList when certain conditions are met in the routine. I suppose that will mean that I need to refactor it so that it returns an object to avoid contention for the TStringList.

What is the approach that has to be done in this case? It seems that it has to do with something called Futures related to TTasks, but I could not really figure out how that must be coded (I did not find any example in Delphi XE7 provided Samples).

J...
  • 30,968
  • 6
  • 66
  • 143
Pep
  • 1,957
  • 2
  • 24
  • 40
  • 1
    Don't you just serialize the list access? Or use producer/consumer approach. It's not clear why there would be a need for a future here. – David Heffernan Mar 15 '15 at 22:41
  • I would serialize the list access because the expectation is that it should only happen once every few thousand runs of the anon procedure, but it is not clear to me how I can use TInterlocked to do that with a list object. – Pep Mar 15 '15 at 23:25
  • It seems I had my blinders on. I can simply use a TMutex or TSemaphore for that. – Pep Mar 15 '15 at 23:46
  • 1
    You could create a different StringList for each thread, i.e. An array of TStringList(each thread uses a different index), then merge the data at the end of the process(if at all needed). This way you will reduce contention by not writing in the same object. – EProgrammerNotFound Mar 16 '15 at 00:01
  • What about TThreadList? http://docwiki.embarcadero.com/CodeExamples/XE6/en/TThreadList_%28Delphi%29 – FMXExpress Mar 16 '15 at 00:59

1 Answers1

2

A future is not applicable here. You should serialize access to the shared data structure with a lock. For instance TMonitor or TCriticalSection.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490