threads at io boundaries

You are working on an Operating System, it has io-wait (epoll, poll, select) primitive, but will only allow you to wait on one type of stream/file at a time.  However it does have threads. Using threads could work but having lots of threads will lead to the normal problems of concurrency. There is no other value of having threads, as you have only one CPU core. You wish you had io-wait, and could implement the code as an event queue.

Therefore,

Put threads at IO boundaries, have these threads wait on one channel each. Communicate with the main thread using pipes. The boundary threads do nothing more that convert IO into piped data. The main thread can then just wait on pipes. All the application logic goes in the main thread.

Each boundary thread, should be input or output, and for one device. It should have no business logic. It should just forward from a pipe to an output, or from an input to a pipe.

Blocking interface rule

Sometime a client wishes to block/wait on/for a service. Sometimes a client wishes to use many services at the same time. Some systems encourage threads as a work around but, threads are difficult, interthread communication is difficult.

Therefore: For all interfaces in a system that block, allow non exclusive blocking. i.e. A client can block on all interfaces at the same time, with a single command. e.g. wait_on([a,b,c,d])

You can use threads at io boundaries to help implement this.

Threads

Threads are dangerous. It is difficult to communicate between threads in a safe way.

Therefore:

Avoid threads unless you have very good language support (e.g. erlang, scoop , posix pipes), except:

  • Fixing interfaces to conform to the blocking interface rule. Then fix at source e.g. threads at io boundaries.
  • Independent threads (no syncronization).

Use one of these safe models of interthread communication:

  • pipes(like Posix), thread safe queues.
  • message passing (like Erlang)
  • transactions (like Scoop)

Don’t use {semaphores, mutexes, monitors} except to implement a safe model of interthread communication.