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:
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():
g_integer_counter: Longint;
On Start Daemon
g_integer_counter := 0;
End On Start Daemon
OnCommandGet
InterLockedIncrement(g_integer_counter);
End OnCommandGet