Jason Mason
Jason Mason

Reputation: 1

Parallelism in threads

Can Parallelism in threads benefit processes in a single CPU system as well? I know they can help in other systems but I was confused if they can actually benefit single CPU systems as well.

Upvotes: 0

Views: 203

Answers (2)

David Schwartz
David Schwartz

Reputation: 182761

Without using threads, you have something of a painful problem. Suppose you're doing something that might block, but probably won't. You can find some asynchronous way to do it, but that can make the programming much more complex, and if it only rarely blocks, the payoff isn't great. But if you do it synchronously, then in the rare cases where it does block, your entire process makes no forward progress.

Page faults are a good example. Consider if a server encounters an error and has to run code to log that error. This code might not be in memory and it might take a disk operation to read that code in. Do you want the whole server to make no forward progress until that disk operation completes?

Upvotes: 1

Peter Cordes
Peter Cordes

Reputation: 364220

You can use threads as a mechanism for async I/O, e.g. to keep multiple disc and network loads in flight, and this works even on a uniprocessor system.

Most OSes have async or non-blocking IO APIs that let you do the same thing from a single-threaded process, but that might be less convenient for some applications.

See What is the status of POSIX asynchronous I/O (AIO)? for some discussion: e.g. event based, non-blocking I/O is usually much better than the "billions of blocking threads" where you have a separate thread for each I/O operation you're waiting on concurrently.

So anyway, yes, you can usefully use threads on a single-core (without hyperthreading or other SMT) system, for things other than number-crunching throughput. (But other ways to get I/O concurrency or parallelism or whatever you want to call it are usually even better.)


There are other use-cases, like a game where one thread runs the GUI and another runs the simulation. Letting the OS schedule them may be simpler than inserting "is it time to update the GUI yet" checks or in the simulation thread.

(Or really anything with a GUI + computation, since you don't want to run the GUI from signal handlers for timers.)

Upvotes: 0

Related Questions