Indy TIdHTTPServer and safe multithread behavior lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- hrayon · 2017-02-15 17:37 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...g_TCriticalSection_Flag: TCriticalSection;g_integer_counter: Integer;...On Start Daemon   g_TCriticalSection_Flag: = TCriticalSection.Create;   g_integer_counter: = 0;End On Start Daemon OnCommandGet   Try      g_TCriticalSection_Flag.Enter;      g_integer_counter: = g_integer_counter + 1;   Finally      g_TCriticalSection_Flag.Leave;   EndEnd OnCommandGet On Stop Daemon  g_TCriticalSection_Flag.Free;End On Stop Daemon ---------------------------------------------------------------------- Remy Lebeau · 2017-02-22 03:27 Quote from: hrayon on February 15, 2017, 06:37:20 pmI 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  ..., IdThreadSafe; g_integer_counter: TIdThreadSafeInteger; On Start Daemon   g_integer_counter := TIdThreadSafeInteger.Create;End On Start Daemon OnCommandGet  g_integer_counter.Increment;End OnCommandGet On Stop Daemon  g_integer_counter.Free;End On Stop Daemon  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i g_integer_counter: Longint; On Start Daemon   g_integer_counter := 0;End On Start Daemon OnCommandGet  InterLockedIncrement(g_integer_counter);End OnCommandGet