Recent

Author Topic: Problem with multi-thread in newer FPC  (Read 7167 times)

fdsingleton

  • New Member
  • *
  • Posts: 31
Problem with multi-thread in newer FPC
« on: January 30, 2018, 07:14:49 am »
I have a general simulation system which runs as Win32 and uses FPC/LCL to compile & run the user-generated problem code.  Much of it was developed in Delphi 3 many years ago, and I switched to FPC so users would not have to have Delphi.  I have attempted to update from FPC 2.6.4 to 3.0.2 or 3.0.4.  I can run simulations with 3.0.4 but the answers are clearly wrong in various unusual ways compared to 2.6.4 which always seemed totally correct on various test problems.  I am at a loss to know what to do.

The FPC program has the initial Windows thread plus 2 computational threads.  I see that the thread programming approach in the manual no longer resembles the traditional Delphi method which remains in my code.  What are the chances that this causes the computational errors?

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Problem with multi-thread in newer FPC
« Reply #1 on: January 30, 2018, 07:35:51 am »
I have a general simulation system which runs as Win32 and uses FPC/LCL to compile & run the user-generated problem code.  Much of it was developed in Delphi 3 many years ago, and I switched to FPC so users would not have to have Delphi.  I have attempted to update from FPC 2.6.4 to 3.0.2 or 3.0.4.  I can run simulations with 3.0.4 but the answers are clearly wrong in various unusual ways compared to 2.6.4 which always seemed totally correct on various test problems.  I am at a loss to know what to do.
There should be no difference. Can you provide example code?
Quote
The FPC program has the initial Windows thread plus 2 computational threads.  I see that the thread programming approach in the manual no longer resembles the traditional Delphi method which remains in my code.  What are the chances that this causes the computational errors?
Even Delphi itself changed a lot from Delphi 3 on the subject of threading. Current thread handling is in line with current Delphi too...
If your code used to work it should still work, though, unless there was always a hidden bug in your code that now manifests itself. I have seen that many times!
More specific regarding assuming I/O buffers are always clean and always synchronized, which technically has never been the case and worked by accident..

Again, we need some simple but complete example code that reproduces the issue. (Note I can still test against Delphi 3 if required)
« Last Edit: January 30, 2018, 07:48:16 am by Thaddy »
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Problem with multi-thread in newer FPC
« Reply #2 on: January 30, 2018, 07:44:40 am »
I have a general simulation system which runs as Win32 and uses FPC/LCL to compile & run the user-generated problem code.  Much of it was developed in Delphi 3 many years ago, and I switched to FPC so users would not have to have Delphi.  I have attempted to update from FPC 2.6.4 to 3.0.2 or 3.0.4.  I can run simulations with 3.0.4 but the answers are clearly wrong in various unusual ways compared to 2.6.4 which always seemed totally correct on various test problems.  I am at a loss to know what to do.

The FPC program has the initial Windows thread plus 2 computational threads.  I see that the thread programming approach in the manual no longer resembles the traditional Delphi method which remains in my code.  What are the chances that this causes the computational errors?
there are two main ways to get different results from multithreading.
1) the data are not properly protected and at some point they get corrupted.
2) the algorithm has problems serializing the calculations, and some calculation occur out of order..
As to why it works on older and not newer versions I have no idea, I'm still on lazarus 1.4, but I would start comparing the locking primitives that I use (critical section, multiread, etc.) before delving in to algorithm land.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Problem with multi-thread in newer FPC
« Reply #3 on: January 30, 2018, 07:49:04 am »
Indeed, posts crossed while I was editing.
Specialize a type, not a var.

fdsingleton

  • New Member
  • *
  • Posts: 31
Re: Problem with multi-thread in newer FPC
« Reply #4 on: January 30, 2018, 09:04:40 pm »
Thank you all for answering.  One of my problems is I have very little time to spend on this project (and do not make any money from it).  I put in a previous post about it last Oct.! 

fdsingleton

  • New Member
  • *
  • Posts: 31
Re: Problem with multi-thread in newer FPC
« Reply #5 on: February 02, 2018, 09:29:59 pm »
Again thanks for answering.  In my program there is the initial Windows thread, which on command starts a main computational thread.  The main computational thread interacts with the Windows thread to produce a log on the screen of what is happening with the computations.  If the user sees a problem and wants to stop the computations he/she clicks on a button which sets a flag, and the next time the computational thread checks the flag it pauses to let the user make adjustments, etc.  Or if the computations actually finish the main computational thread is closed.  All this works correctly in FPC 2.6.4 or FPC 3.0.4. 

However in a years later development I programmed a secondary computational thread which carries out some processing (no I/O or Windows interfacing) parallel to the main computational thread to increase speed.  It calls a separate procedure which is a duplicate of one in the main computational procedure, and there is some logic where whichever thread finishes first has to pause to let its duplicate catch up at which time the secondary computational thread closes and the main one carries on with more computations.  This works with FPC 2.6.4 but NOT with 3.0.2 or 3.0.4 (also works with minor changes with Delphi 3).  The program compiles and runs but the answers are wrong.  There is a simple switch I can set to turn off the parallel processing thread and THEN it gives correct answers in FPC 3.04.  I will continue to look at this and maybe post some code, but I said I don't have much time to work on it.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Problem with multi-thread in newer FPC
« Reply #6 on: February 02, 2018, 09:54:05 pm »
If you don't post any code, we cannot help you.

fdsingleton

  • New Member
  • *
  • Posts: 31
Re: Problem with multi-thread in newer FPC
« Reply #7 on: February 04, 2018, 08:53:57 pm »
Thank you for reading my ramblings.  The problem was TThread.Create has 2 parameters in ObjPascal mode and 1 parameter in Delphi mode, and I somehow left 2 in after I had switched to what I wanted,  Delphi.  No error was issued but it was not working.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Problem with multi-thread in newer FPC
« Reply #8 on: February 04, 2018, 10:22:27 pm »
https://www.freepascal.org/docs-html/rtl/classes/tthread.create.html
The second is a default parameter and can't break code.
Specialize a type, not a var.

 

TinyPortal © 2005-2018