Recent

Author Topic: Queue method called only on thread terminate  (Read 4870 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Queue method called only on thread terminate
« on: July 24, 2017, 10:23:07 am »
hi All,
as per attached i am trying to read what i receive on a socket and push it to main thread for further handling while keeping the socket thread alive.
as per my understanding the synchronize/queue actions allow the created thread to synchronize with main thread.
what i am seeing is that the synchronize/queue is called but only when the created thread is terminated from the main thread.

please help me understand how i can keep the created thread alive and also push to the main thread what i receive on the socket.
thank you


Code: Pascal  [Select][+][-]
  1.   procedure TSockets.DoRun;
  2.   var
  3.     s: string;
  4.     t: TWeb;
  5.  
  6.   begin
  7.     t := TWeb.Create(false);
  8.     t.OnBody:= @WriteBody;
  9.     t.Start;
  10.  
  11.     repeat
  12.       readln(s);
  13.       sleep(200);
  14.     until s = 'x';
  15.  
  16.     writeln('stopping');
  17.  
  18.     t.Terminate;
  19.     {need to do a get to stop the read}
  20.     t.WaitFor;
  21.     Terminate;
  22.   end;

Lazarus 1.8 RC3 64 bit on Windows 7
Lazarus 2.0.2 64b on Debian LXDE 10

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Queue method called only on thread terminate
« Reply #1 on: July 24, 2017, 10:46:26 am »
the right aproach is Not to try to sync threads. use rtlevents ... take. look my code used in the api im writing... search for utramachine in github :) hope it will help you how to use multithread socked server
Speak postscript or die!
Translate to pdf and live!

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Queue method called only on thread terminate
« Reply #2 on: July 24, 2017, 10:49:36 am »
You need to call CheckSynchronize in your main program loop periodically.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Queue method called only on thread terminate
« Reply #3 on: July 24, 2017, 10:57:42 am »
the more you use syncronize the more you lose the efect of multithreding
Speak postscript or die!
Translate to pdf and live!

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Queue method called only on thread terminate
« Reply #4 on: July 24, 2017, 11:28:54 am »
The queue methods are meant and designed to be on thread termination: it is not meant to be used for intermediate results... never has been.
It merely tells the owning (often main) thread "processing is done and I have a result, use it when you are ready". Don't use it for threads that need to keep running: in that case you have to synchronize yourself indeed.

But in general: if you use threads and NEED synchronization, you'd better look at a different solution, like Blestans's.... Cyrax's answer is wrong in this particular case...(that does not happen too often). Popping off the queue in another thread is already threadsafe as long if the thread that is popping is the owner of the thread..
« Last Edit: July 24, 2017, 11:31:55 am by Thaddy »
Specialize a type, not a var.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Queue method called only on thread terminate
« Reply #5 on: July 24, 2017, 01:25:50 pm »
the basic concept is to create N sleeping threads then a new connection is arriving to read the socket and dispatch the new connectiion to a sleeping thread and woke it for processing. when the work  is done the thread goes to sleeping again
Speak postscript or die!
Translate to pdf and live!

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Queue method called only on thread terminate
« Reply #6 on: July 24, 2017, 01:54:09 pm »
the concept is easy. i do not want to create a web server.
i just need to read a redirect from LinkedIN in a Ouath 2 conversation.
i need only one thread to handle one GET, pass the body to parent thread and be ready for next GET.

i am not familiar with RTL events at all.
i will try to look into what they mean, how to use them and go to the repository you mentioned. if you guys could point me in to the right direction with some links to materials it would be much appreciated.

i am just grasping, small steps on my side.
thank you
Lazarus 2.0.2 64b on Debian LXDE 10

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Queue method called only on thread terminate
« Reply #7 on: July 24, 2017, 02:15:33 pm »
just take a look on my code.... its pretty easy to understand.. read utrabackend.pas and utraworker.pas... u will get the idea
Speak postscript or die!
Translate to pdf and live!

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Queue method called only on thread terminate
« Reply #8 on: July 24, 2017, 02:34:26 pm »
try cyrax answer first I think it will solve your problem eg
Code: Pascal  [Select][+][-]
  1.     repeat
  2.       readln(s);
  3.       CheckSynchronize(200);//or what ever fine grain control you require.
  4.       sleep(200);
  5.     until s = 'x';
  6.  
and don't use readln, it will block until an <ln> is read, use keypress and readchar 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

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Queue method called only on thread terminate
« Reply #9 on: July 24, 2017, 03:33:35 pm »
try cyrax answer first I think it will solve your problem eg

*Very* Old quote from Peter Below:
" It queues a method call for the main
 thread to execute, like Synchronize, but it does not wait for it to be
 executed."

Which is *exactly* what I wrote. The TThread.Queue method mentioned in the thread header is already Asynchronous.
If the queue is not empty, it is safe to execute the method or use the data in the Owner. Usually the main thread.
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Queue method called only on thread terminate
« Reply #10 on: July 24, 2017, 04:05:56 pm »
try cyrax answer first I think it will solve your problem eg

*Very* Old quote from Peter Below:
" It queues a method call for the main
 thread to execute, like Synchronize, but it does not wait for it to be
 executed."
OK So far so good, I just don't understand where the conclusion that
The queue methods are meant and designed to be on thread termination: it is not meant to be used for intermediate results... never has been.
comes from, but that is outside the scope of this thread.
Which is *exactly* what I wrote. The TThread.Queue method mentioned in the thread header is already Asynchronous.
If the queue is not empty, it is safe to execute the method or use the data in the Owner. Usually the main thread.
true the queue method is asynchronous. as for the Queue it self the only certain thing, if it is not empty, is that it has one or more methods to be executed, if it is safe to execute them or not is an other matter altogether and it is not addressed by the queue, it can not be addressed by it actually. In this case I assume since the queued method is a method of the running thread, it is only safe as long as the running thread does not get freed before the method is executed.
The main problems of the posted code sample are 2
1) On console application there is no basic "application loop" running that will check the queue for you, unless you call the checksynchronize procedure manually, the queued methods will never be executed.
2) The use of blocking functions like readln will not allow proper synchronization between the two threads and it might lead to false interpretations of the observed results.

The solution is to fix those two points and take an other look on the results.

Now there are a lot more to this thing than that, for example I never looked closely if the queued method access shared data with out locking, the next major group of attention but this comes when the OP hits those problems and can't figure it out, the problem is that those kind of problems are not easily observed so it might take him/her some time to notice them.
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

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Queue method called only on thread terminate
« Reply #11 on: July 24, 2017, 04:50:03 pm »
I agree with Taazz.

The queue methods are meant and designed to be on thread termination: it is not meant to be used for intermediate results... never has been.
It merely tells the owning (often main) thread "processing is done and I have a result, use it when you are ready". Don't use it for threads that need to keep running: in that case you have to synchronize yourself indeed.

But in general: if you use threads and NEED synchronization, you'd better look at a different solution, like Blestans's.... Cyrax's answer is wrong in this particular case...(that does not happen too often). Popping off the queue in another thread is already threadsafe as long if the thread that is popping is the owner of the thread..

Thaddy, I've got a perfect puzzle for you!

 

TinyPortal © 2005-2018