Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Without knowing more about your specific application (ie how long messages take to process etc..) there will be lots of handwaving:</p> <ul> <li><p>Is this managed or unmanaged C++?</p></li> <li><p>Which ThreadPool are you using? </p> <ul> <li>QueueUserWorkItem?</li> <li>Your own pool via CreateIoCompletionPort? </li> <li>Or Vista's SubmitThreadpoolWork?</li> </ul></li> </ul> <p>I think platform is somewhat relevant as the nature of the Thread Pool is important.</p> <p>For example:</p> <p>If you use (<a href="http://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx" rel="nofollow noreferrer">Completion Ports</a>) for your thread pool (ie CreateIoCompletionPort). You have some control on how many threads run concurrently (and hence on how many total threads are ultimately created). If you set the maximum number of concurrent threads to say 4. Windows will will attempt to only allow 4 threads to run concurrently. If all 4 threads are busy processing and you queue a 5th item then windows will not allow that item to run until one of the 4 if finished (reusing the thread). The only time this rule is broken is when threads are blocked (ie waiting on I/O), then more threads are allowed to run.</p> <p>This is the important thing to understand about Completion Ports, and why platform is relevant. It is very difficult to implement something like this without involving the Kernel. Knowing the difference between busy threads and blocked threads requires access to Thread states. Completion ports are very efficient with respect to number of context switches into the Kernel too.</p> <p><strong>Back to your question:</strong></p> <p>It would seem that you should have one thread to process/dispatch the messages and the message processing is all handled by pushing workers onto a thread pool. Let Completion ports handle the load balancing and concurrency. You message processing loop will never block and can continue to process messages. </p> <p>If the rate of incoming messages far exceeds your ability to process them then you will probably have to pay attention to your queue size and block when it grows too large.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThe thread pool is using it's own queue and schelduing mechanism, and C++ is unmanaged. However, I guess it could be converted to completion ports. I must admit that I have very few experience with completion ports, and it seems that they're somewhat under-documented, so if you could suggest a good background link, that would be helpful (MSDN did a somewhat sloppy job for once). I didn't entrily get your suggestion, though. If I have only one thread to dispatch the port and spread the work, then completion ports actually doesn't handle the balancing for me. ----->
      singulars
      1. This table or related slice is empty.
    2. CO<----- From what I know of CP, I can have more threads in the pool than the concurrency, so blocked threads will be compenstated by the extra threads. The question is how can I dynamically extend and shrink the pool so ultimately there will always be threads ready for action?
      singulars
      1. This table or related slice is empty.
    3. COIf you keep a count of how many threads are currently "used" you can extend the number of threads. Basically when GetQueuedCompletionStatus returns, increment a counter, dispatch your processing function and then when it returns decrement the counter. If the counter exceeds the number of threads in your pool add one. The idea is to always keep at least on additional thread in the pool waiting on GetQueuedCompletionStatus. Shrinking the pool is another story. You'll have to decide on how many extra threads are too many. You can use age or number to determine when to destroy excess threads.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload