Are single-threaded applications a dead technology

2020-06-03 04:49发布

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?

13条回答
够拽才男人
2楼-- · 2020-06-03 05:06

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.

查看更多
劫难
3楼-- · 2020-06-03 05:12

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.

查看更多
SAY GOODBYE
4楼-- · 2020-06-03 05:12

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.

查看更多
我想做一个坏孩纸
5楼-- · 2020-06-03 05:13

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)

查看更多
狗以群分
6楼-- · 2020-06-03 05:14

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.

查看更多
何必那么认真
7楼-- · 2020-06-03 05:15

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.

查看更多
登录 后发表回答