Multi-tasking is difficult

Back to John Medina, which I’ve introduced in a previous post, via that youtube link.

His headline about multi-tasking is: You can’t do it (also the title of the post below). It’s true, I’ve read this before in books about the human brain. Apparently, we can only focus on one thing at the time…

 

How would this apply to computing?

In computing we have what we call synchronous or asynchronous programming models. Synchronous is when operations are executed one after another, in the order that the programmer specifies. Say you have:

 

operation A

operation B

operation C

 

You can’t get to execute operation B until you’ve completed A, and you can’t start C until B completes. It’s the simplest programming model, easy to understand and apply. It’s also easy to transpose real life problems into this model. You just have to break it down into consecutive tasks and start from A to Z.

 

As we all know, this model has problems. Even if operation B doesn’t depend on the result or any intermediaries of operation A, still operation B cannot be executed until A finishes. If operation A happens to take a long time, especially if the CPU is idle waiting for operations executed by peripherals to complete (say hard-disk Input/Output), then the model is very wasteful.

 

So the alternative is called, not surprisingly asynchronous programming model. In this model, the programmer analyses the operations involved and see if they are independent of each other. If that’s possible, then the programmer can take advantage of multiple CPUs to execute the code in parallel, or ask one CPU to virtualize the execution, so that gaps are minimized. The CPU will virtualize by switching between the two operations, especially if one of them waits for an operation from a peripheral device.

 

Let’s assume operation A and operation B are independent of each other, while operation C depends on operation A and operation B. Then we have something like this:

 

start operation A

start operation B

– potentially do something else here –

operation A completes

operation B completes

operation C

 

While this model looks (and is) more complex and complicated, it is more efficient in the use of the CPU(s), resulting in increased speed of execution, application/system responsiveness and overall throughput.

Asynchronous programming is a form of multi-tasking, as the programmer attempts to execute multiple tasks/operations at the same time.

 

Multi-tasking programming is much more difficult than single-tasking/synchronous programming model. Still the CPU and the Operating System makes it easier for programmers, by introducing higher level models like threads of execution, by doing a lot of the context switching and scheduling transparently, by introducing programming patterns that can be easily studied/understood/repeated without reinventing the wheel every time.

 

When done by humans, the multi-tasking needs to be all conscious use the brain as our main CPU, and memory as storage for context switches and information about tasks. Switching context however is expensive. Everyone knows how difficult it can be to resume a task when constantly interrupted (emails/phone calls/etc).

And unlike computers, we can’t simply add more CPUs, more RAM although we might be able to overclock. That’s what coffee is for…

Advertisements

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: