Title: Multiple Http Servers and threads Post by: Chris@Xenos 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. Title: Re: Multiple Http Servers and threads Post by: D.Tkalcec (RTC) 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 |