Recent

Author Topic: Indy TIdHTTPServer and safe multithread behavior  (Read 4265 times)

hrayon

  • Full Member
  • ***
  • Posts: 118
Indy TIdHTTPServer and safe multithread behavior
« on: February 15, 2017, 06:37:20 pm »
I want to increment the value of a global variable securely, but the increment routine is within the OnCommandGet method of the object of type TIdHTTPServer. The correct way would be to use the TCriticalSection variable?
See the pseudo-code:
Code: Pascal  [Select][+][-]
  1. ...
  2. g_TCriticalSection_Flag: TCriticalSection;
  3. g_integer_counter: Integer;
  4. ...
  5. On Start Daemon
  6.    g_TCriticalSection_Flag: = TCriticalSection.Create;
  7.    g_integer_counter: = 0;
  8. End On Start Daemon
  9.  
  10. OnCommandGet
  11.    Try
  12.       g_TCriticalSection_Flag.Enter;
  13.       g_integer_counter: = g_integer_counter + 1;
  14.    Finally
  15.       g_TCriticalSection_Flag.Leave;
  16.    End
  17. End OnCommandGet
  18.  
  19. On Stop Daemon
  20.     g_TCriticalSection_Flag.Free;
  21. End On Stop Daemon
  22.  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Indy TIdHTTPServer and safe multithread behavior
« Reply #1 on: February 22, 2017, 04:27:37 am »
I want to increment the value of a global variable securely, but the increment routine is within the OnCommandGet method of the object of type TIdHTTPServer. The correct way would be to use the TCriticalSection variable?

That is one way to do it.  And Indy even have a TIdThreadSafeInteger class for that very purpose:

Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., IdThreadSafe;
  3.  
  4. g_integer_counter: TIdThreadSafeInteger;
  5.  
  6. On Start Daemon
  7.    g_integer_counter := TIdThreadSafeInteger.Create;
  8. End On Start Daemon
  9.  
  10. OnCommandGet
  11.   g_integer_counter.Increment;
  12. End OnCommandGet
  13.  
  14. On Stop Daemon
  15.   g_integer_counter.Free;
  16. End On Stop Daemon
  17.  

On the other hand, if you don't want the overhead of a critical section, you can use atomic APIs instead, like System.InterlockedIncrement():

Code: Pascal  [Select][+][-]
  1. g_integer_counter: Longint;
  2.  
  3. On Start Daemon
  4.    g_integer_counter := 0;
  5. End On Start Daemon
  6.  
  7. OnCommandGet
  8.   InterLockedIncrement(g_integer_counter);
  9. End OnCommandGet
  10.  
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018