* * *

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

hrayon

  • Jr. Member
  • **
  • Posts: 73
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

  • Sr. Member
  • ****
  • Posts: 335
    • 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) open source project - Admin, Developer

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus