Issues with Multithreaded Programming: Part 1

By , June 21, 2011

Multithreaded (Parallel) programming is difficult for many reasons. Developer always has to put extra care to protect the program from issues like race condition, deadlocks, livelocks, priority inversions, two-step dances, and lock convoys. My personal experience says these all issues are tough to identify and then resolve.

It is always better to have understanding on these issues before writing multithreaded application. In this article I have covered the quick overview of following issues associated with multithreaded programming:

Race Condition

A race condition occurs when two or more threads are able to access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any point, we cannot know the order at which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are ‘racing’ to access/change the data.

Often problems occur when one thread does a “check-then-act” (e.g. “check” if the value is X, and then “act” to do something that depends on the value being X) and another thread does something to the value in between the “check” and the “act”.

In order to prevent race conditions occurring, typically put a lock around the shared data to ensure that only one thread can access the data at a time. (Read more about how to put lock and different techniques to synchronize the threads – C#: Thread Synchronization)

 

DeadLock

A deadlock occurs when two or more processes/threads are unable to proceed because each is waiting or one of the others to do something.

A common example is a program communicating to a server, which may find itself waiting for output from the server before sending anything more to it, while the server is similarly waiting for more input from the controlling program before outputting anything.

Another common example in which each process is trying to send stuff to the other but all buffers are full because nobody is reading anything.

Another example, common in database programming, is two processes that are sharing some resource (e.g. read access to a table) but then both decide to wait for exclusive (e.g. write) access.

The real world example will be interactions between humans, as when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they always move the same way at the same time.

 

Other issues of multithreaded programming like “LiveLock“, “Priority Inversion“, “Two-Step Dances” and “Lock Convoys” are covered in “Issues with Multithreaded Programming : Part 2” and  “Issues with Multithreaded Programming – Part 3”.

23 Responses to “Issues with Multithreaded Programming: Part 1”

  1. Pinki says:

    I agree – one should know what will be issue with multithreding application before coding.

    The rece condition and deadlock are very common issues people face…

    nicely explained… thanks

  2. Pankaj says:

    How to prevent from deadlock?

    Could you please add one example?

  3. What’s up mates, its wonderful piece of writing on the topic of tutoringand fully
    explained, keep it up all the time.

  4. Simply dssire to say your article is as astonishing.
    The clarity in your post is simply great and i could assume yyou are an expert on this subject.
    Fine with youyr permission allow me to grab your RSS feed to keep updated with forthcoming post.
    Thanks a million and please carry on the gratifying work.

  5. Impressive business video is capable of getting more likes and comments.
    Effectively, naturally I will have answers to any or all of one’s
    queries. Provides you with the most convenient task management
    along with the easiest control capability.

  6. Dorthy says:

    The software has a Outlook-similar interface and previews messages
    in 3-panes. There are several types of college papers that college professors prefer to give to their students.

    He might even start wondering if you’ve got another guy
    in mind (or may have started dating someone else already).

  7. Carlos says:

    .

    tnx for info!

  8. billy says:

    .

    tnx for info.

  9. Currently it seems like Drupal is the preferred blogging platform available right
    now. (from what I’ve read) Is that what you are using on your blog?

  10. This post is invaluable. When can I find out more?

  11. Arnette says:

    It’s amazing to pay a visit this web page and reading the views off alll
    mates on the topic of this post, while I am also zealous off getting know-how.

  12. Have you ever thought about adding a little bit more
    than just your articles? I mean, what you say is valuable and all.

    But think about if you added some great photos or video clips to
    give your posts more, “pop”! Your content is excellent but with pics and clips,
    this site could certainly be one of the best in its field.
    Great blog!

  13. I enjoy, cause I found just what I used to be taking a look for.
    You have ended my four day lengthy hunt! God Bless you man. Have a great day.

    Bye

  14. Hi there, I log on to your blogs like every week. Your humoristic style is awesome,
    keep doing what you’re doing!

  15. Howdy, i read your blog from time to time and i own a similar one andd
    i was just curious if you gett a llot of spam
    feedback? If so how do you stop it, any plugin or anything
    you can suggest? I get so much lately it’s driving me madd so any help is very much
    appreciated.

  16. Hola , ​​Me gusta leer través de su artículo
    . I gusta para escribir un pequeño comentario para apoyarle.

  17. Appreciation to my father who shared with me on the topic of this webpage,
    this website is really awesome.

  18. Hi there fantastic website! Does running a blog like this
    require a lot of work? I have no understanding of computer programming however I had been hoping to start my
    own blog soon. Anyhow, if you have any suggestions or techniques
    for new blog owners please share. I understand this is off
    topic nevertheless I simply wanted to ask. Many thanks!

  19. Excellent, what a web site it is! This web site provides valuable information to us, keep it up.

  20. Does your site hae a contact page? I’m having trouble locating it but,
    I’d like to send you an e-mail. I’ve got some
    ideas for your blog you might be interessted in hearing. Either way, great website and I look forward to seeing it grow over time.

  21. Woah! I’m really enjoying the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s difficult
    to get that “perfect balance” between usability and appearance.
    I must say you have done a great job with this.

    Also, the blog loads very quick for me on Chrome. Outstanding Blog!

  22. picture fram says:

    Thorn of Girl

    Superb info may be discovered on this website website.

Leave a Reply

*