Recent

Author Topic: custom thread reading file does not respond properly  (Read 8180 times)

Cyrax

  • Hero Member
  • *****
  • Posts: 773
Re: custom thread reading file does not respond properly
« Reply #15 on: September 05, 2017, 02:26:14 pm »
OK, but why?
Is it related to SSD drive? Or OSX?
If it is related to OSX I can add an IFDEF DARWIN, since I have no issues on win and linux.
If it is related ot SSD reading how could I check that my drive is SSD so I have to put a sleep or something?

Like I said, you need to give some time to other threads/processes to do their things. Mainly your program main thread needs time to show your process and do some other processing. As your have noticed, Mac OS definitely need this little pause.

mattias

  • Administrator
  • Full Member
  • *
  • Posts: 153
    • http://www.lazarus.freepascal.org
Re: custom thread reading file does not respond properly
« Reply #16 on: September 05, 2017, 03:46:20 pm »
... example is also wrong. ... works by accident and violates OOP design...
Reason: TThread is a blackbox....

1. OOP contrary to C++ and many other languages allows to do things before calling the inherited constructor. This is by design.
2. As already pointed out a class is initialized (boolean with false). This is by design x-platform.
3. FreeOnTerminate is not a blackbox. It is documented since ages.
4. When CreateSuspended is false, the thread is immediately created and might terminate before returning to the caller. So the FreeOnTerminate must be set before calling inherit or you have a potential memory leak.



mattias

  • Administrator
  • Full Member
  • *
  • Posts: 153
    • http://www.lazarus.freepascal.org
Re: custom thread reading file does not respond properly
« Reply #17 on: September 05, 2017, 03:57:07 pm »
Like I said, you need to give some time to other threads/processes to do their things. Mainly your program main thread needs time to show your process and do some other processing. As your have noticed, Mac OS definitely need this little pause.

You are right, that it is bad practice to poll with 100% CPU.
But the main thread runs fine with another thread using 100% unless you have some strange system configuration.
I tested your example and it works on MacOS with fpc 3.0.2, LCL trunk/carbon/i386. Both with suspended thread and without suspended thread.

How did you start your application on MacOS?

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: custom thread reading file does not respond properly
« Reply #18 on: September 05, 2017, 04:02:55 pm »
... example is also wrong. ... works by accident and violates OOP design...
Reason: TThread is a blackbox....

1. OOP contrary to C++ and many other languages allows to do things before calling the inherited constructor. This is by design.
Good to know. Delphi broke that at some point (2009, 2007 I can't recall) but I had to rewrite some constructors to set properties after the call to inherited at that time.
2. As already pointed out a class is initialized (boolean with false). This is by design x-platform.
3. FreeOnTerminate is not a blackbox. It is documented since ages.
so far so good.
4. When CreateSuspended is false, the thread is immediately created and might terminate before returning to the caller. So the FreeOnTerminate must be set before calling inherit or you have a potential memory leak.
Sorry no. Regardless of the flag in the CreateSupsended parameter the thread is created suspended, if the parameter is false, it is resumed on the OnAfterCreate event after the constructor has exited. So it is safe to set what ever properties you want inside the constructor,  the oncreate event on the other hand I haven't checked.
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

apeoperaio

  • Full Member
  • ***
  • Posts: 165
Re: custom thread reading file does not respond properly
« Reply #19 on: September 05, 2017, 04:12:22 pm »
Like I said, you need to give some time to other threads/processes to do their things. Mainly your program main thread needs time to show your process and do some other processing. As your have noticed, Mac OS definitely need this little pause.

You are right, that it is bad practice to poll with 100% CPU.
But the main thread runs fine with another thread using 100% unless you have some strange system configuration.
I tested your example and it works on MacOS with fpc 3.0.2, LCL trunk/carbon/i386. Both with suspended thread and without suspended thread.

How did you start your application on MacOS?

What do you mean by "How did you start your application on MacOS?"
I launched it both in lazarus and in finder (CPU usage 54%, checked using activity monitor).
I am using lazarus 1.6.4, fps 3.0.2, OSX Sierra and SSD.

Thaddy

  • Hero Member
  • *****
  • Posts: 9425
Re: custom thread reading file does not respond properly
« Reply #20 on: September 05, 2017, 04:52:55 pm »
... example is also wrong. ... works by accident and violates OOP design...
Reason: TThread is a blackbox....

1. OOP contrary to C++ and many other languages allows to do things before calling the inherited constructor. This is by design.
2. As already pointed out a class is initialized (boolean with false). This is by design x-platform.
3. FreeOnTerminate is not a blackbox. It is documented since ages.
4. When CreateSuspended is false, the thread is immediately created and might terminate before returning to the caller. So the FreeOnTerminate must be set before calling inherit or you have a potential memory leak.
You look silly... Not to put a too fine point on it...
What happens - in any OOP language - when the inherited constructor resets a field?
Just because your are used to it does not mean it is correct.
And a derived class *must* respect what its parent does. Black box....

Documenting out of order is ok, but in this case not. It is a basic element of the class structure...
I didn't even know experienced programmers like you would fall in that trap!. (Well... I actually knew)

Don't pollute this thread (sic) anymore... Let's ask Joost: he wrote the shitty part... And maybe reconsider...? Because I am right and you are wrong.
« Last Edit: September 05, 2017, 05:01:36 pm by Thaddy »
also related to equus asinus.

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: custom thread reading file does not respond properly
« Reply #21 on: September 05, 2017, 05:40:45 pm »
4. When CreateSuspended is false, the thread is immediately created and might terminate before returning to the caller. So the FreeOnTerminate must be set before calling inherit or you have a potential memory leak.
Sorry no. Regardless of the flag in the CreateSupsended parameter the thread is created suspended, if the parameter is false, it is resumed on the OnAfterCreate event after the constructor has exited. So it is safe to set what ever properties you want inside the constructor,  the oncreate event on the other hand I haven't checked.

Maybe not all platforms?
Interesting bug report in the comment:
Code: Pascal  [Select]
  1. procedure TThread.AfterConstruction;
  2. begin
  3. //fpc\3.0.2\source\rtl\objpas\classes\classes.inc
  4.   inherited AfterConstruction;
  5. // enable for all platforms once http://bugs.freepascal.org/view.php?id=16884
  6. // is fixed for all platforms (in case the fix for non-unix platforms also
  7. // requires this field at least)
  8. {$if defined(unix) or defined(windows) or defined(os2) or defined(hasamiga)}
  9.   if not FExternalThread and not FInitialSuspended then
  10.     Resume;
  11. {$endif}
  12. end;

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: custom thread reading file does not respond properly
« Reply #22 on: September 05, 2017, 05:45:38 pm »
OK, but why?
Is it related to SSD drive? Or OSX?
If it is related to OSX I can add an IFDEF DARWIN, since I have no issues on win and linux.
If it is related ot SSD reading how could I check that my drive is SSD so I have to put a sleep or something?

Like I said, you need to give some time to other threads/processes to do their things. Mainly your program main thread needs time to show your process and do some other processing. As your have noticed, Mac OS definitely need this little pause.

Is that a known Mac OS design limitation (or feature)?

Same code works fine on Windows, even with more CPU intensive code. The main GUI thread gets enough time share to respond properly.

Cyrax

  • Hero Member
  • *****
  • Posts: 773
Re: custom thread reading file does not respond properly
« Reply #23 on: September 05, 2017, 06:27:09 pm »
OK, but why?
Is it related to SSD drive? Or OSX?
If it is related to OSX I can add an IFDEF DARWIN, since I have no issues on win and linux.
If it is related ot SSD reading how could I check that my drive is SSD so I have to put a sleep or something?

Like I said, you need to give some time to other threads/processes to do their things. Mainly your program main thread needs time to show your process and do some other processing. As your have noticed, Mac OS definitely need this little pause.

Is that a known Mac OS design limitation (or feature)?

Same code works fine on Windows, even with more CPU intensive code. The main GUI thread gets enough time share to respond properly.

I don't know as I do not have Mac OS X to test properly. So I assume that it is odd limitiation (perhaps a bug in its kernel or a bug in Lazarus/FPC?) of Mac OS X considering that it is a unix environment.

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: custom thread reading file does not respond properly
« Reply #24 on: September 05, 2017, 11:19:16 pm »
4. When CreateSuspended is false, the thread is immediately created and might terminate before returning to the caller. So the FreeOnTerminate must be set before calling inherit or you have a potential memory leak.
Sorry no. Regardless of the flag in the CreateSupsended parameter the thread is created suspended, if the parameter is false, it is resumed on the OnAfterCreate event after the constructor has exited. So it is safe to set what ever properties you want inside the constructor,  the oncreate event on the other hand I haven't checked.

Maybe not all platforms?
Interesting bug report in the comment:
Code: Pascal  [Select]
  1. procedure TThread.AfterConstruction;
  2. begin
  3. //fpc\3.0.2\source\rtl\objpas\classes\classes.inc
  4.   inherited AfterConstruction;
  5. // enable for all platforms once http://bugs.freepascal.org/view.php?id=16884
  6. // is fixed for all platforms (in case the fix for non-unix platforms also
  7. // requires this field at least)
  8. {$if defined(unix) or defined(windows) or defined(os2) or defined(hasamiga)}
  9.   if not FExternalThread and not FInitialSuspended then
  10.     Resume;
  11. {$endif}
  12. end;
As far as I know MACOS is part of the UNIX definition.
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