Quiet Musings

Worker and I/O Threads

Code

Notes from these posts:

Other good posts:

Synchronous and Asynchronous Programming

  • Synchronous programming: the thread leaves its current task and starts working on a new task immediately
  • Asynchronous programming: the thread continues working on its current task
    • With asynchronous execution, your don't always need another thread to handle multiple tasks, it can use a queue to execute tasks to be completed once the current task is finished

What are I/O Threads?

  • Not everything in a program will consume CPU time. For tasks like reading data from a file on disk or sending TCP/IP requests, a thread will delegate the work to a device (e.g. a hard disk or network adapter) and then wait for the results.
  • Threads that are asleep, waiting for results are costly. They can take up a couple MB of memory and thread switching becomes slower as the thread count grows. It is more efficient to have a thread continue to work, and then have a callback once the delegated work is finished.
  • An I/O thread is an abstraction over the delegated work given to the devices

Database connection pools

  • Database connection pools are similar to thread pools
  • The main purpose of a db connection pool is to maintain a number of connections that the application can use (and reuse) when it needs to execute queries to the db
  • Since connections are costly to create/close it is preferred to keep them alive while needed