views:

1433

answers:

13

I just bought a new, sub-US$1,000 laptop, one aimed squarely at the consumer, non-developer market and, looking over the specs, was surprised to find that it came standard with a dual-core processor.

This led me to the question: with multicore machines becoming the norm, is it ever correct to write a single-threaded application anymore?

Excepting trivial applications, which can reasonably be expected to fit entirely within a single core of a single processor of the weakest system on which it will run, will an application which runs in all one thread be seriously degraded by the way modern OSs spread their execution across cores when no guidance is given by the application as to how to optimize such a split?

+43  A: 

You shouldn't be multi-threading unless the application needs to be multi-threaded.

Just because you can multi-thread doesn't mean you should. Multiple cores/processors doesn't really change this fact.

Geoffrey Chetwood
I know the old canards as well as anyone. The question breaks down (in part) to "Has the definition of 'needs multithreading' changed?
Jekke
@Jekke: And my answer is no. There is no difference.
Geoffrey Chetwood
Couldn't agree more, +1
Treb
I should add, that if you are even considering adding threads to an application for the reason that people have multiple cores, and not because of sound judgment born of a distinct *need* for threading, you really have no business using threads at all.
Geoffrey Chetwood
+1 strongly agree. Shiny new toys != design requirements
Steven A. Lowe
Donald Knuth said in an interview that of all the programs he had written, only one could have benefited from multi-threading. Maybe.
Carl Seleborg
@Carl: Indeed. It is amazing the level of ignorance that exists regarding threading.
Geoffrey Chetwood
"Just because you can, doesn't mean you should" This applies to just about everything
Makach
+46  A: 

Since threading always adds extra complexity to applications, I believe that single threaded applications will always have their place.

Not even when single core processors are completely obsolete will single threaded programming be gone.

Dual cores, especially in the consumer market, are great for multitasking. If every app takes every processor core it possibly can we will run into the same problems we had with single core processors.

I say don't go nuts and start mulithreading everything. Keep it in one thread unless there is a good reason not too.

jjnguy
Also, sometimes you don't want your program to use all of the CPU. Enterprise inventory software, for example generally, can't significantly impact the usability of the machine. In turn, taking on the complexity of threading is not worthwhile.
Paul Whitehurst
Exactly !
jjnguy
Never forget that multiple single-threaded applications can make use of multiple cores. Don't be afraid to delegate complexity to the OS.
Steve Schnepp
+2  A: 

Definetely, parallel programming will advance a lot in the future since CPU are not going to become much faster anymore, we simply will have lots of them. Having dual or quad cores is just the beginning, there will be systems with a much higher number of processors in the near future (currently there are CPU with 256 cores, Windows 7 will have support for such processors).

However, I think that parallelism is not suitable for solving any problem and it requires a different style of programming. Using current technology, multi-threading adds a quite high level of complexity to an application. Therefore paradigms such as functional programming as well as libraries/compilers supporting parallelization will become more and more important in the future.

David Callahan had an interesting article in MSDN magazine on the shift in design considerations regarding parallel programming: Paradigm Shift: Design Considerations For Parallel Programming

0xA3
+1  A: 

As usual in performance optimization; you can't make generic statements about whether something is "performant enough" about arbitrary applications. The question is, is it "performant enough" for application X.

However, perhaps more to your point to continue to see the benefits of Moore's law which is no longer making processors faster but making them smaller (and hence more cores on a chip) applications will have to become multi-threaded.

However, the cost of building a multi-threaded application (engineering, testing, support) are still significant so you really shouldn't do it unless you need the performance. I think coming down the pike are some easier ways to do multi-threaded programming with some various libraries as well as things like functional programming and optimized compilers so I think this will get easier as we continue the transition into a multi-core world.

Peter Oehlert
+4  A: 

Multi-threaded applications are significantly more difficult to write, and maintain. I think you will start seeing more applications which leverage multi-threading thanks to advances in both hardware, but also in advances in the technology stack.

Microsoft has a set of extensions that I believe are being added into the runtime called ??Parallels?? I know they call the LINQ enabled version PLINQ. Essentially it tries to remove a lot of the error prone plumbing from multi-threaded algorithims.

Only time tell however, and I would wager there were still be plenty of single threaded applications out there because they won't algorithims that justify the performance cost of spinning up threads.

JoshBerke
+4  A: 

... will an application which runs in all one thread be seriously degraded by the way modern OSs spread their execution across cores when no guidance is given by the application as to how to optimize such a split?

No, it will still run just as well as it used run to on a single-core CPU.

ChrisW
I'm tempted to make this the accepted answer because I think it's the only one that points out a critical fact in answering the question: Whatever default slicing the OS is doing, it will only make the processing worse than on a single-core machine in severely degenerate cases.Well done.
Jekke
agreed!!! except when the two processes will spend cpu cycles "waiting" for external (to the box) resourses like a printer, or and internet connection, or a database, or ...
Charles Bretana
Threading is not about performance, it is about handling blocking situations... That is the part Jekke truly doesn't seem to understand here...
Geoffrey Chetwood
@Rich threading and multiple CPUs have many effects: handling blocking (i.e. if a thread must block, then let it be a worker thread and not the UI thread which blocks); increase capacity (process different parts of the same data on several CPUs concurrently); and avoid being preempted.
ChrisW
@ChrisW: I understand, but thinking that performance on multi-core machines is a reason to thread is a fundamental flaw, and that is what I am trying to address in Jekke's thinking.
Geoffrey Chetwood
There is at least one exception to this, which I consider a bug in the OS. Some versions of WinOS will move the app from core to core in round robin fashion. This means each core is 50% busy. So the power management decides the the computer is 50% idle and it lower the clock speed to save power.
Die in Sente
@Die in Sente: This is not a bug, this is how task scheduling works.
Geoffrey Chetwood
+2  A: 

Multiple cores are advertised for the users benefit, not the programmer. A user will be told that having multiple cores means they can run a number of complex applications at the same time, e.g. updating a large spreadsheet in Excel and viewing a Powerpoint presentation etc.

Obviously as a developer you CAN make good use of the extra cores; most video editing packages run far faster on multi-core machines.

If you are writing an application that would seriously benefit from using more cores, then it may well be worth investing the additional development time, as most new machines now have more than one core, but as always, think about the ROI.

ck
A: 

The answer to your question is tricky, because it asks us to speculate on future developement. The simple answer to your question is this:

No, single threads are the basis for multiple threads, you can't have one without the other. Thus, the capacity for single threaded applications will always exist, and they will doubtlessly be written for years to come.

The complex answer is as follows: The reason for computers having dual core processors standard is because its the next step in processor developement. while many applications will not take immediate advantage of the dual core, some will. As such, a company like intel must accomodate, or be squashed by the competition.

Beyond this, there are immediate advantages. Larger addressable memory space. Operating Systems themselves already being optomized for the dual also reaps immediate benefits.

Also: If you have a library that is optimized for the dual core processors all subsequent developement will benefit. An example is searching and sorting algorithms (a nartural choice for "thread split" due to the divisibility of the data) which can be optimized for the dual core, and then be perpectually used with the advantage.

windfinder
+3  A: 

Hardware doesn't really change the fact that any task in a particular application is going to block or not block, and that's really what's going to determine if you should use multithreading or not.

However, modern programming languages are adding new features to make it easier and safer for the programmer to implement threaded applications. Cocoa for example has NSOperationQueue which abstracts the worker thread model, and there are more improvements coming in the next release of OS X. In addition to making it easier to implement a simple threading model, NSOperationQueue also manages threads in a way that the total number of threads is appropriate for the number of processors and cores, so if your application uses a lot of threads there can be some pretty significant speed gains, without too much extra work.

Marc Charbonneau
+2  A: 

Programming for multiple threads is currently somewhat difficult, error-prone, and difficult to test (testing for race conditions is not easy). Therefore, it should be reserved for when the advantages overcome the disadvantages.

Lots of programs run fast enough for almost all purposes, and don't need any performance improvement. Some take a while, but for reasons that don't really involve the CPU and aren't going to be improved by multiple threads. For these, using more than one thread makes them harder to develop while not helping the end user.

In the meantime, multiple cores can be useful without multithreading. Most operating systems have multiple processes going (monitoring, background housekeeping, viruses, spyware, spambots, whatever) and allowing these to run on another core can free up one for user code.

David Thornley
Finding race conditions thru testing isn't just "not easy". In the general case it is impossible.
Die in Sente
@Die in Sente: You make me wish I could downvote comments.
Geoffrey Chetwood
+1  A: 

Lets face it most applications are written for business, they access a database and allow data in it to be viewed, edited and appended. Then maybe some data analysis reports and so on.

Other than a separate UI thread if the database queries are going to take a while there is very little reason for threading it since the gains will be fractional and largely un-noticed by the end user. There are maybe a few cases where a parallel loop would benefit applications maybe.

I believe the real benefit of multiple cores at the moment is muliple applications accessing different cores at the same time. Just think how many processes windows runs in the background, these can be run concurrently across any number of cores.

PeteT
A: 

Maybe the question should be reformulated to: "is running a program on one only core a dead technology". That would make it a lot more general. If you look at the recent added web workers added to firefox 3.5 and other major browsers, then they do support multiple cores, but using processes and message passing. A much saner approach than threading. In the end it all depends on the problem at hand. Using multiple cores really only makes a difference if the problem at hand is CPU bound.

I would also like to add that your application might not be the only running process on the machine (hint).

Anders Rune Jensen
A: 

A good way to use multiple processors without multithreading:

cat data.txt | sed 's/,/ /g' | awk '{print $4}' | gzip > foo.txt.gz

Does that answer your question on weather dual core means all applications should be multithreaded? The above can in theory saturate 3 CPUs. (excluding cat, only using it because that makes the line more readable)

Thomas