[ACCEPTED]-What kinds of applications need to be multi-threaded?-multithreading

Accepted answer
Score: 24

There is no hard and fast answer, but most 20 of the time you will not see any advantage 19 for systems where the workflow/calculation 18 is sequential. If however the problem can 17 be broken down into tasks that can be run 16 in parallel (or the problem itself is massively 15 parallel [as some mathematics or analytical 14 problems are]), you can see large improvements.

If 13 your target hardware is single processor/core, you're 12 unlikely to see any improvement with multi-threaded 11 solutions (as there is only one thread at 10 a time run anyway!)

Writing multi-threaded 9 code is often harder as you may have to 8 invest time in creating thread management 7 logic.

Some examples

  • Image processing can often be done in parallel (e.g. split the image into 4 and do the work in 1/4 of the time) but it depends upon the algorithm being run to see if that makes sense.
  • Rendering of animation (from 3DMax,etc.) is massively parallel as each frame can be rendered independently to others -- meaning that 10's or 100's of computers can be chained together to help out.
  • GUI programming often helps to have at least two threads when doing something slow, e.g. processing large number of files - this allows the interface to remain responsive whilst the worker does the hard work (in C# the BackgroundWorker is an example of this)

GUI's are an interesting 6 area as the "responsiveness" of the interface 5 can be maintained without multi-threading 4 if the worker algorithm keeps the main GUI 3 "alive" by giving it time, in Windows API 2 terms (before .NET, etc) this could be achieved 1 by a primitive loop and no need for threading:

MSG msg;
while(GetMessage(&msg, hwnd, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);

    // do some stuff here and then release, the loop will come back
    // almost immediately (unless the user has quit)
}
Score: 15

Servers are typically multi-threaded (web servers, radius 13 servers, email servers, any server): you 12 usually want to be able to handle multiple 11 requests simultaneously. If you do not 10 want to wait for a request to end before 9 you start to handle a new request, then 8 you mainly have two options:

  1. Run a process with multiple threads
  2. Run multiple processes

Launching a 7 process is usually more resource-intensive 6 than lauching a thread (or picking one in 5 a thread-pool), so servers are usually multi-threaded. Moreover, threads 4 can communicate directly since they share 3 the same memory space.

The problem with multiple 2 threads is that they are usually harder 1 to code right than multiple processes.

Score: 8

There are really three classes of reasons 2 that multithreading would be applied:

  • Execution Concurrency to improve compute performance: If you have a problem that can be broken down into pieces and you also have more than one execution unit (processor core) available then dispatching the pieces into separate threads is the path to being able to simultaneously use two or more cores at once.
  • Concurrency of CPU and IO Operations: This is similar in thinking to the first one but in this case the objective is to keep the CPU busy AND also IO operations (ie: disk I/O) moving in parallel rather than alternating between them.
  • Program Design and Responsiveness: Many types of programs can take advantage of threading as a program design benefit to make the program more responsive to the user. For example the program can be interacting via the GUI and also doing something in the background.

Concrete 1 Examples:

  • Microsoft Word: Edit document while the background grammar and spell checker works to add all the green and red squiggle underlines.
  • Microsoft Excel: Automatic background recalculations after cell edits
  • Web Browser: Dispatch multiple threads to load each of the several HTML references in parallel during a single page load. Speeds page loads and maximizes TCP/IP data throughput.
Score: 5

These days, the answer should be Any application that can be.

The speed 13 of execution for a single thread pretty 12 much peaked years ago - processors have 11 been getting faster by adding cores, not 10 by increasing clock speeds. There have 9 been some architectural improvements that 8 make better use of the available clock cycles, but 7 really, the future is taking advantage of 6 threading.

There is a ton of research going 5 on into finding ways of parallelizing activities 4 that we traditionally wouldn't think of 3 parallelizing. Even something as simple 2 as finding a substring within a string can 1 be parallelized.

Score: 4

Basically there are two reasons to multi-thread:

  1. To 43 be able to do processing tasks in parallel. This 42 only applies if you have multiple cores/processors, otherwise 41 on a single core/processor computer you 40 will slow the task down compared to the 39 version without threads.

  2. I/O whether that 38 be networked I/O or file I/O. Normally 37 if you call a blocking I/O call, the process 36 has to wait for the call to complete. Since 35 the processor/memory are several orders 34 of magnitude quicker than a disk drive (and 33 a network is even slower) it means the processor 32 will be waiting a long time. The computer 31 will be working on other things but your 30 application will not be making any progress. However 29 if you have multiple threads, the computer 28 will schedule your application and the other 27 threads can execute. One common use is 26 a GUI application. Then while the application 25 is doing I/O the GUI thread can keep refreshing 24 the screen without looking like the app 23 is frozen or not responding. Even on a 22 single processor putting I/O in a different 21 thread will tend to speed up the application.

The 20 single threaded alternative to 2 is to use 19 asynchronous calls where they return immediately 18 and you keep controlling your program. Then 17 you have to see when the I/O completes and 16 manage using it. It is often simpler just 15 to use a thread to do the I/O using the 14 synchronous calls as they tend to be easier.

The 13 reason to use threads instead of separate 12 processes is because threads should be able 11 to share data easier than multiple processes. And 10 sometimes switching between threads is less 9 expensive than switching between processes.

As 8 another note, for #1 Python threads won't 7 work because in Python only one python instruction 6 can be executed at a time (known as the 5 GIL or Global Interpreter Lock). I use 4 that as an example but you need to check 3 around your language. In python if you 2 want to do parallel calculations, you need 1 to do separate processes.

Score: 3

Many GUI frameworks are multi-threaded. This 11 allows you to have a more responsive interface. For 10 example, you can click on a "Cancel" button 9 at any time while a long calculation is 8 running.

Note that there are other solutions 7 for this (for example the program can pause 6 the calculation every half-a-second to check 5 whether you clicked on the Cancel button 4 or not), but they do not offer the same 3 level of responsiveness (the GUI might seem 2 to freeze for a few seconds while a file 1 is being read or a calculation being done).

Score: 3

All the answers so far are focusing on the 31 fact that multi-threading or multi-processing 30 are necessary to make the best use of modern 29 hardware.

There is however also the fact 28 that multithreading can make life much easier 27 for the programmer. At work I program software 26 to control manufacturing and testing equipment, where 25 a single machine often consists of several 24 positions that work in parallel. Using multiple 23 threads for that kind of software is a natural 22 fit, as the parallel threads model the physical 21 reality quite well. The threads do mostly 20 not need to exchange any data, so the need 19 to synchronize threads is rare, and many 18 of the reasons for multithreading being 17 difficult do therefore not apply.

Edit:

This is 16 not really about a performance improvement, as 15 the (maybe 5, maybe 10) threads are all 14 mostly sleeping. It is however a huge improvement 13 for the program structure when the various 12 parallel processes can be coded as sequences 11 of actions that do not know of each other. I 10 have very bad memories from the times of 9 16 bit Windows, when I would create a state 8 machine for each machine position, make 7 sure that nothing would take longer than 6 a few milliseconds, and constantly pass 5 the control to the next state machine. When 4 there were hardware events that needed to 3 be serviced on time, and also computations 2 that took a while (like FFT), then things 1 would get ugly real fast.

Score: 2

Not directly answering your question, I 19 believe in the very near future, almost 18 every application will need to be multithreaded. The 17 CPU performance is not growing that fast 16 these days, which is compensated for by 15 the increasing number of cores. Thus, if we will want our applications 14 to stay on the top performance-wise, we'll 13 need to find ways to utilize all your computer's 12 CPUs and keep them busy, which is quite 11 a hard job.

This can be done via telling 10 your programs what to do instead of telling 9 them exactly how. Now, this is a topic I 8 personally find very interesting recently. Some 7 functional languages, like F#, are able 6 to parallelize many tasks quite easily. Well, not 5 THAT easily, but still without the necessary 4 infrastructure needed in more procedural-style 3 environments.

Please take this as additional 2 information to think about, not an attempt 1 to answer your question.

Score: 1

The kind of applications that need to be threaded 3 are the ones where you want to do more than 2 one thing at once. Other than that no application 1 needs to be multi-threaded.

Score: 1

Applications with a large workload which 8 can be easily made parallel. The difficulty 7 of taking your application and doing that 6 should not be underestimated. It is easy 5 when your data you're manipulating is not 4 dependent upon other data but v. hard to 3 schedule the cross thread work when there 2 is a dependency.

Some examples I've done 1 which are good multithreaded candidates..

  • running scenarios (eg stock derivative pricing, statistics)
  • bulk updating data files (eg adding a value / entry to 10,000 records)
  • other mathematical processes
Score: 0

E.g., you want your programs to be multithreaded 7 when you want to utilize multiple cores 6 and/or CPUs, even when the programs don't 5 necessarily do many things at the same time.

EDIT: using 4 multiple processes is the same thing. Which 3 technique to use depends on the platform 2 and how you are going to do communications 1 within your program, etc.

Score: 0

Although frivolous, games, in general are 4 becomming more and more threaded every year. At 3 work our game uses around 10 threads doing 2 physics, AI, animation, redering, network 1 and IO.

Score: 0

Just want to add that caution must be taken 9 with treads if your sharing any resources 8 as this can lead to some very strange behavior, and 7 your code not working correctly or even 6 the threads locking each other out.

mutex 5 will help you there as you can use mutex 4 locks for protected code regions, a example 3 of protected code regions would be reading 2 or writing to shared memory between threads.

just 1 my 2 cents worth.

Score: 0

The main purpose of multithreading is to separate time domains. So 3 the uses are everywhere where you want several 2 things to happen in their own distinctly 1 separate time domains.

Score: 0

HERE IS A PERFECT USE CASE

If you like affiliate 28 marketing multi-threading is essential. Kick 27 the entire process off via a multi-threaded 26 application.

Download merchant files via 25 FTP, unzipping the files, enumerating through 24 each file performing cleanup like EOL terminators 23 from Unix to PC CRLF then slam each into 22 SQL Server via Bulk Inserts then when all 21 threads are complete create the full text 20 search indexes for a environmental instance 19 to be live tomorrow and your done. All 18 automated to kick off at say 11:00 pm.

BOOM! Fast 17 as lightening. Heck you have so much time 16 left you can even download merchant images 15 locally for the products you download, save 14 the images as webp and set the product urls 13 to use local images.

Yep I did it. Wrote 12 it in C#. Works like a charm. Purchase 11 a AMD Ryzen Threadripper 64-core with 256gb 10 memory and fast drives like nvme, get lunch 9 come back and see it all done or just stay 8 around and watch all cores peg to 95%+, listen 7 to the pc's fans kick, warm up the room 6 and the look outside as the neighbors lights 5 flicker from the power drain as you get 4 shit done.

Future would be to push processing 3 to GPU's as well.

Ok well I am pushing it 2 a little bit with the neighbors lights flickering 1 but all else was absolutely true. :)

More Related questions