5000 threads lazarus / 61-Beginners ---------------------------------------------------------------------- tudi_x · 2018-05-14 07:07 hi! please advise if it would be an issue with Lazarus creating 5000 threads simultaneously (on ubuntu) for devops purposes aka checking services on the infrastructure. i understand it is about 10 Gb RAM but RAM is cheap nowadays. (i do not know if i want to work with a thread pool at 5000 and maintain that amount of RAM occupied) if the number is an issue, what would be the maximum that is stable. thank you ---------------------------------------------------------------------- af0815 · 2018-05-14 07:41 Threads are more CPU issue. Howw many Cores have the machine ? The CPU and the Sytemmust switch between the processes = Threads I think 5000 Threads sounds like a bad design. Andreas ---------------------------------------------------------------------- Thaddy · 2018-05-14 08:21 Well there is a little more to it. In principle 5000 threads is not such a big problem (some webservers are in that ball park). There are some issues that you need to take care of: - Thread stack allocation: On Windows, the default is rather huge. Use the compiler switches to limit it. - Context switching: try to avoid that with such a number of threads. Do NOT let the threads interact with each other: information sharing is a bad idea with so many threads. - CPU cores and virtual cores: How many do you have? Devide 5000/cpu cores and using fibers per core or virtual core is usually more efficient. ---------------------------------------------------------------------- Pascal · 2018-05-14 08:39 Why not? As they are only checking services they are idle most of the time, arent't they? You can also limit them to only run a specfied number simultaneously by using a semaphore. So if you have a CPU with 8 threads you can limit them to 8 simultaneoiusly running. ---------------------------------------------------------------------- tudi_x · 2018-05-14 08:47 from a requirements perspective i have to check that let us say (x > 100): - x database instances have upgraded correctly by checking their version; - x database instances are up every hour by running some SQL script; so from a solution stand point what are my options? ---------------------------------------------------------------------- Pascal · 2018-05-14 09:47 Build a list of "work to be done" with time stamp of execution. Run 8 threads that pick up a "work to be done" with time stamps older than "now" and handle it and perhaps add a new "work to be done" with new time stamp for the next check (next hour). ---------------------------------------------------------------------- Thaddy · 2018-05-14 09:48 Well. That depends on the database software. In such a scenario and using Oracle -clustered - my above scenario should work pretty well on even very high demands... (read: been there, done that.. ). I would not attempt that with Sqlite or Firebird.... Possibly MySql, MariaDB and PostGress are good enough, though, to handle such a load. ---------------------------------------------------------------------- Thaddy · 2018-05-14 09:50 Quote from: Pascal on May 14, 2018, 11:47:54 amBuild a list of "work to be done" with time stamp of execution. Run 8 threads that pick up a "work to be done" with time stamps older than "now" and handle it and perhaps add a new "work to be done" with new time stamp for the next check (next hour). That is not relevant in a real-time scenario, which he explained and is referring to. You need a proper database back end to handle such load. ---------------------------------------------------------------------- Pascal · 2018-05-14 10:45 Quote from: Thaddy on May 14, 2018, 11:50:19 amQuote from: Pascal on May 14, 2018, 11:47:54 amBuild a list of "work to be done" with time stamp of execution. Run 8 threads that pick up a "work to be done" with time stamps older than "now" and handle it and perhaps add a new "work to be done" with new time stamp for the next check (next hour). That is not relevant in a real-time scenario, which he explained and is referring to. You need a proper database back end to handle such load. "every hour" doesn't sound like real-time scenario ---------------------------------------------------------------------- Thaddy · 2018-05-14 12:21 Indeed. But then you would not need 5000 threads but just two. That's how I read it. In the context of, say, a webserver 5000 threads is a valid proposition. In the context of database operations a decent database should be able to maintain integrity. In that case the programming should be done database side. ---------------------------------------------------------------------- Pascal · 2018-05-14 12:32 Quote from: Thaddy on May 14, 2018, 02:21:20 pmIndeed. But then you would not need 5000 threads but just two. That's how I read it. In the context of, say, a webserver 5000 threads is a valid proposition. In the context of database operations a decent database should be able to maintain integrity. In that case the programming should be done database side. Okay, right. In fact you'll never need 5000 threads for this monitoring tasks, even if you have to monitor 5000 db instances. Except they provide some kind of notification for which the thread could wait. (I've done this for monitoring hundreds of directories on the network for changes) ---------------------------------------------------------------------- mdalacu · 2018-05-14 17:32 I also created a monitor software with ~100 working threads running through a job queue in loop. I have a problem on Windows to create over 110 threads...on linux (fpc) and Delphi(windows) i do not have such issue. I coudn't found a soution. ---------------------------------------------------------------------- ASerge · 2018-05-14 20:10 Quote from: mdalacu on May 14, 2018, 07:32:38 pmI have a problem on Windows to create over 110 threads...on linux (fpc) and Delphi(windows) i do not have such issue. I coudn't found a soution. Very strange. This simple example quite to itself works under windows and takes up around 140 MB of memory. 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 {$MODE OBJFPC}{$APPTYPE CONSOLE}program Project1; uses Classes; type  TSimpleThread = class(TThread)  private    FWaitEvent: PRTLEvent;    FCounter: PLongInt;  protected    procedure Execute; override;  public    constructor Create(AWaitEvent: PRTLEvent; ACounter: PLongInt);  end; constructor TSimpleThread.Create(AWaitEvent: PRTLEvent; ACounter: PLongInt);begin  inherited Create(False);  FreeOnTerminate := True;  FWaitEvent := AWaitEvent;  FCounter := ACounter;end; procedure TSimpleThread.Execute;begin  if Assigned(FCounter) then    InterLockedIncrement(FCounter^);  if Assigned(FWaitEvent) then  begin    RTLeventWaitFor(FWaitEvent);    RTLeventSetEvent(FWaitEvent);  end;  if Assigned(FCounter) then    InterLockedDecrement(FCounter^);end; const  CMaxThreads = 5000;var  WaitEvent: PRTLEvent;  i: Integer;  Counter: LongInt = 0;begin  WaitEvent := RTLEventCreate;  for i := 1 to CMaxThreads do  begin    TSimpleThread.Create(WaitEvent, @Counter);    Write('.');  end;  Writeln;  Writeln(CMaxThreads, ' threads added. Press Enter to close this threads');  Readln;  RTLEventSetEvent(WaitEvent);  while Counter > 0 do    Writeln(Counter, ' treads left');  Writeln('All added threads are closed. Press Enter to exit');  RTLEventDestroy(WaitEvent);  Readln;end.