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”.

39 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.

  23. I just couldn’t depart your website before suggesting that I really enjoyed the standard info an individual provide for your visitors?

    Is gonna be again incessantly to check up on new posts

  24. I was pretty pleased to uncover this website.

    I need to to thank you for your time due to this wonderful read!!
    I definitely enjoyed every bit of it and i also have you
    saved to fav to look at new stuff on your blog.

  25. Souls in the Waves

    Superior Early morning, I just stopped in to go to your site and assumed I’d say I appreciated myself.

  26. Whispering Misty

    So sorry you’ll pass up the workshop!

  27. The Absent Game

    Involving me and my husband we’ve owned much more MP3 players over time than I can count, which includes Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few many years I’ve settled down to one line of gamers.

  28. I blog frequently and I seriously thank you for your information. This
    great article has really peaked my interest. I will take a note of your site and keep checking for new information about once per week.
    I opted in for your RSS feed too.

  29. If you are going for most excellent contents like me,
    only pay a quick visit this web site all the time for the reason that it presents quality contents, thanks

  30. You actually make it seem so easy together with your presentation however
    I in finding this matter to be really something which I beliece I might nevber understand.
    It kind of feels too complex and extremely large foor
    me. I’m having a look forward in your subsequent put up, I will try to get the
    cling of it!

  31. Great post. I was checking continuously this blog and I am impressed!

    Extremely helpful info specially the last part :) I care for such information a
    lot. I was seeking this particular information for a very long time.
    Thank you and good luck.

  32. My Homepage says:

    … [Trackback]

    [...] Informations on that Topic: ashishkhandelwal.arkutil.com/csharp/issues-with-multithreaded-programming-part-1/ [...]

  33. Thanks for some other great article. Where else may anyone get that kind of info in such an ideal approach of writing?

    I have a presentation subsequent week, and I’m at the
    look for such info.

  34. Just desire to say your article is as surprising.
    The clearness in your post is simply cool and i could assume you are an expert on this
    subject. Well with your permission allow me to grab your feed to keep updated with forthcoming post.
    Thanks a million and please keep up the gratifying work.

  35. Julienne says:

    Its not my first time to payy a quick visit this site,
    i am browsing this website dailly and take fastidious facts from here everyday.

  36. Poi c’è la possibilità di completare gli obiettivi proposti
    dal gioco, che in premio offrono proprio diamanti.

  37. Nevertheless we at Android Veterans have created a cheat for Shadow Battle 2, which allows you to have an infinite amount of gems,
    cash and vitality. In the event you beloved this text and you wish
    to get hold of extra particulars pertaining to insane hackz – -struggle-2-cash-hack-wonderful-suggestions-…

    kindly pay a visit to our web-page.

Leave a Reply

*