Recent

Author Topic: My bug or rtl bug with TThread and TThreadList?  (Read 5551 times)

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
My bug or rtl bug with TThread and TThreadList?
« on: July 28, 2017, 04:56:21 pm »
Could someone please look at this small bit of code and tell me I have a bug or if there is something wrong with the RTL?

https://gist.github.com/sysrpl/40505d25fbd38c4404f95225f21fe0a6

SafePostThread is called from the UI thread, but a complete hangs of either thread happens after sometimes one or two calls.

The most I get before things go wrong this:

Post message
New message
Post message

I am using FPC trunk on Linux.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #1 on: July 28, 2017, 05:05:53 pm »
are you using postmessage on linux? Don't! A couple of years back there was a thread about that in here, I think it was proven that postmessage processes the message in the threads space instead of the main thread space, making it unsuitable for use not even in single thread occations. Let me see if I can fine the thread for you.

here is thread in question http://forum.lazarus.freepascal.org/index.php/topic,30031.0.html.
« Last Edit: July 28, 2017, 05:10:49 pm by taazz »
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

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #2 on: July 28, 2017, 05:10:01 pm »
Well as of right now PostMessage is not being used at all. Just a thread is executing, well failing to execute.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #3 on: July 28, 2017, 05:12:38 pm »
Well as of right now PostMessage is not being used at all. Just a thread is executing, well failing to execute.
Yeah, I have doubts on how thread safe writeln is too. I'll test the unit later tonight and report my findings. Have you tried to use asyncqueue or something along those lines?
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

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #4 on: July 28, 2017, 05:19:11 pm »
Well as of right now PostMessage is not being used at all. Just a thread is executing, well failing to execute.
Yeah, I have doubts on how thread safe writeln is too. I'll test the unit later tonight and report my findings. Have you tried to use asyncqueue or something along those lines?
Even if I protected the WriteLns with a critical section it's still bugged.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #5 on: July 28, 2017, 06:19:43 pm »
Well as of right now PostMessage is not being used at all. Just a thread is executing, well failing to execute.
Yeah, I have doubts on how thread safe writeln is too. I'll test the unit later tonight and report my findings. Have you tried to use asyncqueue or something along those lines?
Even if I protected the WriteLns with a critical section it's still bugged.
I do not thing serializing the calls to writeln makes it thread safe. I would start by doing a test with the thread calling the tthread's queue method and call the checksynchronize in the console application's loop to see what is going on, or even better use a gui app that incorporates the call to checksynchronize in its loop and see if that helps at all.

Keep in mind that queue and checksynchronise are just a mechanism to execute arbitrary code in the main thread and the simplest safe way to post some feedback on what is going on. It has its problems ee do not queue methods of objects that are about to be destroyed do not post data/memory addresses that are accessed from the secondary threads and you will be safe.

a Quick look reveled.
Code: Pascal  [Select][+][-]
  1. procedure TMessageThread.Execute;
  2. var
  3.   M: TMessage;
  4. begin
  5.   FreeOnTerminate := True;
  6.   while not Terminated do
  7.   begin
  8.     Sleep(10);
  9.     repeat
  10.       M := nil;
  11.       FList.LockList;                //<- entercritical section counter =1
  12.       M := FList.LockList.First;//<- entercritical section counter =2
  13.       FList.UnlockList; //<- entercritical section counter =1 the list will be permanently locked nothing can get in.
  14.       if M <> nil then
  15.       begin
  16.         // PostMessage(M.Wnd, M.Msg, 0, 0);
  17.         WriteLn('New message');
  18.         FList.Remove(M);
  19.         M.Free;
  20.       end;
  21.     until M = nil;
  22.   end;
  23.   FList.Free;
  24. end;
  25.  
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: 14373
  • Sensorship about opinions does not belong here.
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #6 on: July 28, 2017, 06:36:11 pm »
Don't chain, or be aware you are chaining:
Code: Pascal  [Select][+][-]
  1.       FList.LockList;                //<- entercritical section counter =1
  2.       M := FList.LockList.First;//<- entercritical section counter =2 || chained call... You should call Flist.First. Or simply call this, but not the above..
  3.  
Is simply programmer error.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #7 on: July 28, 2017, 06:52:30 pm »
Thaddy,

Got it. Thanks.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #8 on: July 28, 2017, 09:12:10 pm »
Thaddy,

Got it. Thanks.
should I assume it is fixed or should I go on and build a test app?
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

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #9 on: July 28, 2017, 09:32:31 pm »
Yeah, I have doubts on how thread safe writeln is too.
It isn't. At least, not in 3.0.2 on windows. No reason to panic though, just garbled output and/or output in wrong order.

See also logger attachment with example in this post. (sorry for the faulty critical section: there should be as many as there are calls to writeln or code should be re-arranged to have only one overloaded function that actually calls writeln).

Just remove/comment the threadsafe define and run the example a couple of times to see where/how things go wrong.
« Last Edit: July 28, 2017, 09:47:18 pm by molly »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #10 on: July 29, 2017, 12:17:20 am »
Yeah, I have doubts on how thread safe writeln is too.
It isn't. At least, not in 3.0.2 on windows. No reason to panic though, just garbled output and/or output in wrong order.

See also logger attachment with example in this post. (sorry for the faulty critical section: there should be as many as there are calls to writeln or code should be re-arranged to have only one overloaded function that actually calls writeln).

Just remove/comment the threadsafe define and run the example a couple of times to see where/how things go wrong.
thank you. I missed that post. I'll take a closer look tomorrow night.
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: 14373
  • Sensorship about opinions does not belong here.
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #11 on: July 29, 2017, 12:38:33 pm »
Yeah, I have doubts on how thread safe writeln is too.
It isn't. At least, not in 3.0.2 on windows. No reason to panic though, just garbled output and/or output in wrong order.

See also logger attachment with example in this post. (sorry for the faulty critical section: there should be as many as there are calls to writeln or code should be re-arranged to have only one overloaded function that actually calls writeln).

Just remove/comment the threadsafe define and run the example a couple of times to see where/how things go wrong.
thank you. I missed that post. I'll take a closer look tomorrow night.

Note on windows it is not threadsafe, but it is on linux, bsd and mac. Windows has just one *1* console. But there is code around that mitigates this (I use the code by Barry Kelly)
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #12 on: July 30, 2017, 06:07:15 pm »
Thank you for the hint Thaddy, it is a nice solution.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #13 on: July 30, 2017, 08:20:46 pm »
Note on windows it is not threadsafe, but it is on linux, bsd and mac. Windows has just one *1* console. But there is code around that mitigates this (I use the code by Barry Kelly)
I have no idea what you are talking about, I have various links about multi threading and bary kelly but nothing that shows any code about console thread safety and a not so quick search did not reveal anything relative might be my searching terms though. In any case thanks for the hint.
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: 14373
  • Sensorship about opinions does not belong here.
Re: My bug or rtl bug with TThread and TThreadList?
« Reply #14 on: July 30, 2017, 08:23:39 pm »
Note on windows it is not threadsafe, but it is on linux, bsd and mac. Windows has just one *1* console. But there is code around that mitigates this (I use the code by Barry Kelly)
I have no idea what you are talking about, I have various links about multi threading and bary kelly but nothing that shows any code about console thread safety and a not so quick search did not reveal anything relative might be my searching terms though. In any case thanks for the hint.
You need to improve on your Google art.
I thought it was already here? https://forum.lazarus.freepascal.org/index.php/topic,37321.msg250540.html#msg250540   .... Look at Twrite... <very grumpy   >:D >:D >:D>
Originally sourced from http://codeverge.com/embarcadero.delphi.win32/threads/1039644

Oh, well....<sigh, fully deserved sigh I think, getting tired of such idi...>

Btw: that code is now cross platform...tnx to me myself and I.... O:-) Just kidding: Barry Kelly... 8-)
« Last Edit: July 30, 2017, 10:10:34 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018