Library tutorials & articles

Handling multiple socket read & write operations

Non blocking dispatch

To ensure that users wishing to dispatch a work item to the thread pool can do so without blocking we implement the user level dispatch function as follows:

  void CThreadPool::Dispatch(
      ULONG_PTR completionKey,
      DWORD dwNumBytes /*= 0*/,
      OVERLAPPED *pOverlapped /*= 0*/)
  {
      if (completionKey == 0)
      {
        throw CException(_T("CThreadPool::Dispatch()"), _T("0 is an invalid value for completionKey"));
      }
      m_dispatchPort.PostStatus(completionKey, dwNumBytes, pOverlapped);
  }

The restriction on 0 valued completion keys is unfortunate but allows us to shut down the thread pool's dispatch thread by posting a 0 to its completion port. The thread pool now has two IO Completion Ports. The dispatch port is serviced by a single maintenance thread which executes the HandleDispatch() method to dispatch work items to the worker threads. Users dispatch without blocking and the maintenance thread dispatches in a blocking manner so that it can expand the thread pool when it needs to. The work item port is serviced by a variable number of threads. We've seen how we know when we need to expand the number of threads, now we'll look at how we reduce the number of threads when the work load is low.

Comments

  1. 08 May 2007 at 11:15
    It's up to you. You may select anyone you want, but better except some standart ports. Read documentation.
  2. 13 Feb 2004 at 01:46

    Hi there,


    How do I find the port of the machine for scoket connection.


    Thanks


    PPCC

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Handling multiple socket read & write operations.

Leave a comment

Sign in or Join us (it's free).

Len Holgate Len has been programming for over 20 years, having first started with a Sinclair ZX-80. Now he runs his own consulting company, JetByte Limited. JetByte provides contract programming and consultanc...

Related discussion

Related podcasts

  • Listener Feedback 67

    This mailbag episode includes FASM, scripts, sockets, SSL/TLS, HTTPS, Windows 7's XP mode, and more. Security Now wiki shownotes For 16kpbs versions, transcripts, and notes (including fixes), visit Steve's site: grc.com, also the home of the best disk maintenance and recovery utility ever written...

We'd love to hear what you think! Submit ideas or give us feedback