RTC Forums
April 29, 2024, 01:31:23 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Multiple Http Servers and threads  (Read 3197 times)
Chris@Xenos
RTC Expired
*
Posts: 3


« on: July 19, 2017, 03:55:05 PM »

Hi Danijel,

First of all, excellent product!
I just wanted to ask some some clarifications about how threads are used.

If I want in a single exe to have more than one HttpServers, lets say three or four,
each one will run in its own thread or will they all run in the main thread?
If they run all in the main thread is there a downside and should I make them run
each one in separate thread?
Also I read in a recent post about the variables controlling the threadpool of the framework,
are those threads of that pool used one for each request received to execute the code
in that event? IS this the thread model you are using?

Thanks.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: July 19, 2017, 07:41:57 PM »

In a nutshell, there is only one pool of RTC "Worker Threads" (RTC-specific "TThread" implementation) per process, created and managed by the RTC Thread Pool (see "rtcThrPool.pas" unit) and used to execute "Jobs" (see "TRtcJob" class) from "Virtual Threads" (see "TRtcThread" class).

Each multi-threaded RTC component creates a "Virtual Thread", then posts all their "Jobs" to their "Virtual Thread". Every "Virtual Thread" has a "Job Queue", which allows multiple "Jobs" to be posted to the queue without any delays, then executed in sequence (first-in, first-out).

RTC Thread Pool takes care of creating "Worker Threads" (depending on RTC Thread Pool configuration) and dispatching "Jobs" from "Virtual Threads" to "Worker Threads". There is no direct relation between a "Virtual Thread" and a "Worker Thread", which means that any "Worked Thread" can execute a "Job" from any "Virtual Thread" (one at a time) and any number of "Virtual Threads" can share one or more "Worker Threads".

For example, when you are using the "TRtcHttpServer" component with the "MultiThreaded" property set to TRUE, the main Server component will be creating one "Virtual Thread" for use by the Server listener after calling the "Listen" method, then the Server listener component will be creating new "TRtcHttpServer" components, each with its own "Virtual Thread", for every connection open to the Server by any Client.

In other words, every connection on the Server is a separate "TRtcHttpServer" instance, using its own "Virtual Thread" to execute its code from background threads. All code related to the same connection will be running from the same "Virtual Thread", but NOT necessarily from the same "Worker Thread", because "Worked Threads" are being assigned "Jobs" from "Virtual Theeads" by the RTC Thread Pool, which has to ensure that all "Virtual Threads" get an equal chance to have their "Jobs" executed, even if the number of "Worker Threads" is lower than the number of "Virtual Threads".

NOTE: Single-threaded components, like TRtcHttpClient and TRtcHttpServer components with their MultiThreaded property is set to FALSE (default), do NOT use the RTC Thread Pool, but execute all their code in the Main Thread, or from the Thread where components methods are called (user code).

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.023 seconds with 16 queries.