Recent

Author Topic: [solved] Sharing variables - TThread  (Read 1959 times)

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
[solved] Sharing variables - TThread
« on: April 30, 2016, 05:37:01 pm »
Hi, I have two threads, the main thread and the client thread. The purpose of the client thread is to provide non-blocking request-reply network functionality (using ZeroMQ lib). Creating and starting the client thread goes like this:

Code: Pascal  [Select][+][-]
  1. //from the main thread
  2. FClientThread.Create('tcp://localhost:5020');
  3. FClientThread.Start;

Sending a request goes like this:
Code: Pascal  [Select][+][-]
  1. //from the main thread
  2. FClientThread.SendRequest('im_requesting_stuff',-1,'give_it_to_me');

Some of the variables are not being synchronized even waiting for them like so:

Code: Pascal  [Select][+][-]
  1. procedure TClientThread.SendRequest(ACode: string; ATrialIndex: integer;
  2.   ARequest: string);
  3. begin
  4.   // BUG:These vars are being filled with the last values
  5.   FRequest := ARequest;
  6.   FCode := ACode;
  7.   FTrialIndex := IntToStr(ATrialIndex);
  8.   RTLeventSetEvent(FRTLEvent);
  9. end;
  10.  
  11. procedure TClientThread.Execute;
  12. var
  13.   AMessage : UTF8String;
  14. begin
  15.   while not Terminated do
  16.     begin
  17.       AMessage := '';
  18.       RTLeventWaitFor(FRTLEvent);
  19.  
  20.       FRequester.send( FRequest );
  21.       FRequester.recv( AMessage );
  22.  
  23.       // BUG:These vars are being filled with the last values
  24.       // ('FTrialIndex', 'AMessage', 'FCode')
  25.       FMsg := #40#39 + FTrialIndex + #39#44#32#39 + AMessage + #39#44#32#39 + FCode + #39#41;
  26.       Synchronize( @Showstatus );
  27.     end;
  28. end;  
  29.  
  30.  

Strangely enough, the commented bug occurs ONLY if SendRequest is called multiple times in very short intervals.

Any suggestions?

Best Regards,
Rafael
« Last Edit: May 04, 2016, 09:37:56 am by carlitus »
Be mindful and excellent with each other.
https://github.com/cpicanco/

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Be mindful and excellent with each other.
https://github.com/cpicanco/

 

TinyPortal © 2005-2018