Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Implementing threading in a program is hard, yes, however why is it that some people will not implement them even when there is an obvious need for it.

An example: Program has to load a dataset from a database, the thing to do would be to make the connection and get the data from the database in a worker thread and then load it into the GUI, leaving the GUI thread responsive for the user.

But no, I've talked to people whom seem to think that threads are evil and bad and whatnot and one should avoid them at all costs. I've even heard that some class instructor advised against to use of threads and therefore did not want cover their use. WHAT???

With the hardware going into multi-core, I think that we need to understand threads better and not be afraid to use them. I find it a fascinating subject personally.

So what are things you've heard about threading that are false?

share|improve this question
    
Misfits and underachievers cannot handle threads. The real question is: what are you going to do about that? –  Job Feb 3 '12 at 20:44
3  
Those are not false ideas, but threads should always be avoided. Do your architecture correctly so that the threading support has already been correctly handled and every programmer do not need to do it themselves. Once programmers learn to add a thread every situation you'll have a big problems. –  tp1 Feb 3 '12 at 20:53
add comment

closed as not constructive by gnat, Walter, kevin cline, ChrisF Feb 7 '12 at 12:47

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

7 Answers

up vote 18 down vote accepted

Threading is hard

Sure. It can be. However, people get this idea in their head that it's so hard, such that they don't bother trying to figure it out.

It's not like it's impossible.

share|improve this answer
2  
I support this answer. People think it's hard. However it's not when you spend enough time trying to understand. –  user2567 Dec 22 '10 at 7:30
10  
@Pierre, I would expect that lots of people's definition of hard is "you have to spend enough time trying to understand". –  Benjol Dec 22 '10 at 10:16
1  
Threading is getting to be a lot easier with TPL and await/async keywords :) –  Rachel Feb 3 '12 at 20:27
    
@Pierre 303: When you spend enough time to understand it's still hard, and in fact the people who understand it best are the most likely to avoid it as much as possible. –  Michael Borgwardt Aug 31 '12 at 12:26
add comment

It's not the threading part that's hard but the need for synchronization and everything else that comes with using threads. In your GUI example how do you tell the main thread the dataset is ready to be accessed? Do you pass around a whole bunch of callbacks? Do you scatter a whole bunch of check variables throughout your code? In some GUI models, e.g. Silverlight, there's something called thread affinity which means you don't get to access GUI elements sitting on the main thread from other threads so you have to get out of your way to let the main thread know certain information is ready to be processed further.

I haven't really heard any false things about threads. I've just read a whole bunch of situational case studies about synchronization being a bitch when whatever algorithm you are using isn't inherently parallel.

share|improve this answer
    
Note to self: write parallel algorithms...thanks. –  Droogans Dec 4 '11 at 15:07
    
Message queues (same as in MFC). However, getting programmers not to sabotage the message queue (by directly sharing data in memory), even when it's a fireable offense, seems to have failed. –  rwong Oct 15 '12 at 4:39
add comment

Threading solves all your problems

If you're having performance issues you should not jump right to threading.

Threads are lightweight

Threads are lightweight in tens and twenties. Spawning thousands of threads is not.

Threading is easy [Java]

It's easy to create threads, that doesn't mean you will benefit from it.

share|improve this answer
    
Just for the record, Mac OS won't let you (on a default install) create more than 512 threads. –  zneak Dec 21 '10 at 23:36
1  
That really depends on your language. Spawning 1 million threads in Erlang is barely noticeable, even on an older laptop, let alone a modern server. And actually, these aren't even just threads, they are full-blown processes, i.e. much heavier than threads. In addition to their own program counter and call stack (which are pretty much the only things a thread has), they also have their own heap and even their own garbage collector. –  Jörg W Mittag Dec 22 '10 at 1:31
4  
@Jörg W Mittag: I am confused by your comment. How does erlang change how the OS creates a thread or process? –  Steve Evers Dec 22 '10 at 6:47
1  
@SnOrfus: Erlang doesn't use OS threads. There are three main Erlang implementations right now: BEAM, HiPE and Erjang. BEAM and HiPE are native implementations (that can even run without any OS at all) and implement their own processes. Erjang runs on the JVM and implements processes using the fantastically brilliant Kilim library. –  Jörg W Mittag Dec 22 '10 at 13:35
    
@Jörg W Mittag: Considering my question programmers.stackexchange.com/questions/28453/…, I find this very interesting. Thank you. –  Steve Evers Dec 23 '10 at 4:44
add comment

You will eventually loose any gains from threading because fixing crazy bugs that will arise from use of some libraries/functions that are not thread safe (what you were not aware of) will require excessive synchronization.

You have much higher probability of encountering bug that you will not be able to fix if you use threads then when you don't.

share|improve this answer
    
Unfixable bugs? I've not seen one of those before.. –  adamk Dec 22 '10 at 15:44
    
You really have never seen a bug that you could not fix? In the time and for the pay that was available? –  Kamil Szot Dec 22 '10 at 22:31
add comment

To summarize point wise why threads are difficult to use :-
True Things 1) Need the synchronization, and careful design decisions about what to lock and when to lock
2) No control on the run time flow
3) Difficult debugging
4) (Very few times) platform compatibility :- Libraries do exists to take care of this

False Things : -
1) Confusing concepts of thread-safe and re-entrant functions
2) Threads looks good on the paper but are very hard to implement

share|improve this answer
    
Are these supposed to be true or untrue? The OP asked about what things that are not true, and scare people away from doing multi-threaded programming. –  Steve Evers Dec 22 '10 at 6:41
    
actually you don't need locks or synchronization. There are also message passing models (e.g. erlang, scala), and STM models (e.g. clojure). And on top of that there are thread safe data structures that don't need locks (ConcurrentHashMap in java) and atomic primitives that don't require locks. –  Kevin Feb 3 '12 at 20:50
add comment

If you don't want to write tests for your code, then don't use threads.

Threads are not for the typical 'copy and paste' programmer who does not understand underlying fundamentals of OS and computer architecture. Since 90% of programmers are only familiar with Java, these really are not the people who should be using threads. Java makes threads "easy" but I've seen a lot of programmers who just think that if they used synchronized structures their code will work in threads .... uhm no.

That being said, everyone needs to start somewhere, just don't make your first threading project upgrading your companies production backend server.

share|improve this answer
    
Can you recommend some resources on how to do threads correctly? –  Jonathan Feb 2 '12 at 15:23
    
I'm sure this has been addressed. Try starting here stackoverflow.com/questions/660621/threading-best-practices –  Casey Feb 3 '12 at 20:18
    
The problem with this is that even if you have 100% test coverage you will have no way of knowing if your tests will cover all possible issues with how instructions interleave with shared resources. On the other hand with a shared nothing architecture it becomes much easier. –  Zachary K Apr 20 '12 at 12:22
add comment

An example: Program has to load a dataset from a database, the thing to do would be to make the connection and get the data from the database in a worker thread and then load it into the GUI, leaving the GUI thread responsive for the user.

I don't see that this situation represents a necessity to use threading for at least 4 reasons:

  1. The data retrieval should be very fast.

  2. In many Line of Business applications, the user has nothing to do with the application in the 1 second or two he/she is waiting for the result. Also, the user will have to wait until the data comes back any way to complete the desired task. The query on the other hand could be coded intelligently such that it retrieves only a page-full of information at a time and other optimization techniques could help the response time.

  3. In web based interfaces, links may be made active regarding of the threading model.

  4. Threading adds complexity as you admit, some developers down the line may not be able to add features or debug complex code.

My opinion is: Use threading when you must because software maintainability and reliability is more valuable to an organization than code elegance.

share|improve this answer
1  
Your first point reminds me of the fallacies of distributed computing ( en.wikipedia.org/wiki/Fallacies_of_Distributed_Computing ). An awful lot of users may begin to wildly click around when they have to wait more than 1 or 2 seconds from your point 2 for an unresponsive interface, making things worse. –  Secure Feb 4 '12 at 8:03
    
@Secure, the link is interesting, thanks for sharing it. I am not sure we could ever capture the user's focus all the time on interface or even on the entire job in this day and age. I agree with you that in E-Business sites, you don't want the user to go away at all. –  Emmad Kareem Feb 4 '12 at 10:30
    
I'm not talking about the focus. When the user clicks on a button and the interface just freezes because the database is queried, without some visual response that something is done, some users try to click the button again. And again. Then try to click other buttons or options. I've seen admins doing this who should know better. –  Secure Feb 5 '12 at 6:42
    
Even worse when the result screen is first drawn, but shown empty. Don't know about most current versions, but the search result in older Outlooks is a good bad example. When starting the search, it shows "Result set is empty" or something like this, for some seconds with a large search base, showing the first results when found. If you're too impatient or in a hurry, you've already switched to the next folder, believing that nothing's there. –  Secure Feb 5 '12 at 6:46
    
@Secure, I see your point. What you are describing here shows a good example of an inconsistent user interface. What you have described also occurs when you search for files. But what is the answer to this other than telling the user that the search has already begun? –  Emmad Kareem Feb 5 '12 at 7:04
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.