Friday, April 13, 2012

What's a Thread?

One of the challenges for beginner programmers is adapting to the vocabulary in their field. 

The first time I heard the term "multi threaded" I had absoolutely no clue.  Looking up the definition didn't help much either.  At the time, I simply couldn't get over the feeling of being out of my depth.

Although it was a number of years ago, I suppose the same thing happens to beginning computer programmers.  I bet a good number of them up reading a definition similar to the following from Wikipedia at the time of this writing:
In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources. In particular, the threads of a process share the latter's instructions (its code) and its context (the values that its variables reference at any given moment). To give an analogy, multiple threads in a process are like multiple cooks reading off the same cook book and following its instructions, not necessarily from the same page.

On a single processor, multithreading generally occurs by time-division multiplexing (as in multitasking): the processor switches between different threads. This context switching generally happens frequently enough that the user perceives the threads or tasks as running at the same time. On a multiprocessor (including multi-core system), the threads or tasks will actually run at the same time, with each processor or core running a particular thread or task.
Many modern operating systems directly support both time-sliced and multiprocessor threading with a process scheduler. The kernel of an operating system allows programmers to manipulate threads via the system call interface. Some implementations are called a kernel thread, whereas a lightweight process (LWP) is a specific type of kernel thread that shares the same state and information.

Why isn't the above definition very useful to beginners and only marginally useful to anyone else?

In my opinion, these kinds of definitions are not very useful to anybody because if you can comprehend it then you are already higher up on the learning curve that anyone who would actually need it in the first place.
The problem is that an understanding of threads is not the result of processing the words that make up its definition.  Rather it comes from an understanding of the environment that naturally gives rise to their existance.

How can this be improved upon?
Here's a first stab at a more concise way to saying all this.  The main difference is taht I try to use only words that a beginner would understand. 
  • Computers run programs one instruction at a time.
  • An operating system is a special program that orchestrates the execution of multiple other programs by organizing their instructions in memory and switching rapidly between them at a rate that gives the user an effect of simultaneous execution.
  • A thread is to a process as a process is to an operating system.
It is still up to the student to have a fair amount of mental acuity to process a more abstract description particularly the use of an analogy, so I'm not entirely sure this is an improvement but I do think it's in the direction of one.

No comments:

Post a Comment