Article 34 - Programming Threads

The concept of thread programming can be a disaster if the programmer does not consider certain constructs, namely, thread safety, deadlocks, and starvation.

While it is simple to write a program that uses multiple threads to perform a job, it is extremely difficult to write a program that guarantees the job is performed well and without errors. Often the case, multi-threading can lead to deadlocks, that is no thread can advance, while other threads may be starved waiting.

There are many uses to making a program multi-threaded. For example, a webserver that needs to handle multiple requests can be multi-threaded to handle simultaneous requests. This works well if the machine is multi-cored, so each core can be busy operating. This may not work as well if there are too many executing threads, since context switching, the act of saving the current thread's execution and resuming the execution of another thread, is expensive.

But there are many advantages for a multi-threaded program. It can increase resource utilization and throughput. Also, it can load share between mulitple core machines.

Thread safety is important, especially, when threads share a global resource, often the case, a lock. If a thread accesses a shared resource, another thread should not be accessing that same resource at the same time, unless all simultaneous operations are read. If any one of the operations is a write, an unexpected result may occur. These important sharing sections is normally protected with locks and called the "critical section." Furthermore, even if these critical sections are safely guarded, one thread may have changed a shared resource that another thread does not expect.

Multithreaded programs can always cause debugging annoyances because context switching can occur at any time, thus repeating an issue is not often possible. Race conditions may dynamically change the program behaviour unexpectedly as well.

Deadlock is another important multi-threaded issue. A deadlock occurs if all of the following four conditions hold true: a thread is holding a resource, there is no preemption, there is a circular wait dependency, and the resource that is being waited on is held by a thread stuck in the circular dependency.

All of these four criterias are required for a deadlock to occur.

Deadlocks can be prevented, or resolved. To resolve a deadklock, simply reset each thread that is stuck, either by revoking the shared resource, preemption, or terminating a thread and restarting it again. On the other hand, preventing deadlocks can be challenging. Sophisticated algorithms are available, which is beyond the scope of this article.

Comments (23)

Posted by anonymous - mulberry bags at Monday, November 19, 2012 12:33 AM

Merely a smiling visitant here to share the love (:, btw outstanding layout.
[url=http://www.bagmulberry.co.uk]mulberry bags[/url]
<a href="http://www.bagmulberry.co.uk" title="mulberry bags">mulberry bags</a>

Posted by anonymous - ugg bailey button at Monday, November 19, 2012 8:08 AM

I got what you intend, appreciate it for putting up.Woh I am thankful to find this website through google. "Being intelligent is not a felony, but most societies evaluate it as at least a misdemeanor." by Lazarus Long.
[url=http://www.superclassicboots.com/ugg-1873-bailey-button-c-1.html]ugg bailey button[/url]
<a href="http://www.superclassicboots.com/ugg-1873-bailey-button-c-1.html" title="ugg bailey button">ugg bailey button</a>

Posted by anonymous - Lose Stomach Fat at Tuesday, November 20, 2012 3:27 AM

Merely wanna input that you have a very decent site, I like the design it actually stands out.
[url=http://5tipsto-losestomachfat.blogspot.com]Lose Stomach Fat[/url]
<a href="http://5tipsto-losestomachfat.blogspot.com" title="Lose Stomach Fat">Lose Stomach Fat</a>

Posted by anonymous - mulberry bags at Tuesday, November 20, 2012 5:14 AM

Its fantastic as your other blog posts : D, regards for posting . "The rewards for those who persevere far exceed the pain that must precede the victory." by Ted W. Engstrom.
[url=http://pinterest.com/daniel0128/mulberry-bags/]mulberry bags[/url]
<a href="http://pinterest.com/daniel0128/mulberry-bags/" title="mulberry bags">mulberry bags</a>

Posted by anonymous - cheap designer bags at Wednesday, November 21, 2012 3:38 PM

But a smiling visitor here to share the love (:, btw outstanding style and design. "He profits most who serves best." by Arthur F. Sheldon.
[url=http://www.bagsamazon.info/]cheap designer bags[/url]
<a href="http://www.bagsamazon.info/" title="cheap designer bags">cheap designer bags</a>

Posted by anonymous - cheap supra shoes at Thursday, November 22, 2012 6:45 AM

Simply wanna say that this is very beneficial , Thanks for taking your time to write this.
[url=http://www.supratkstore.com]cheap supra shoes[/url]
<a href="http://www.supratkstore.com" title="cheap supra shoes">cheap supra shoes</a>

Posted by anonymous - beats headphones at Thursday, November 22, 2012 7:02 AM

You have brought up a very good points , regards for the post.
[url=http://www.headphonesamazon.com/]beats headphones[/url]
<a href="http://www.headphonesamazon.com/" title="beats headphones">beats headphones</a>

Posted by anonymous - Christian Louboutin shoes at Thursday, November 22, 2012 3:14 PM

Hey, you used to write magnificent, but the last few posts have been kinda boring?I miss your super writings. Past few posts are just a little out of track! come on!
[url=http://www.mychristianlouboutinonline.com/]Christian Louboutin shoes[/url]
<a href="http://www.mychristianlouboutinonline.com/" title="Christian Louboutin shoes">Christian Louboutin shoes</a>

Posted by anonymous - UGG ??? at Thursday, November 22, 2012 6:04 PM

I simply could not go away your site prior to suggesting that I really loved the standard info a person supply in your visitors? Is gonna be again continuously to inspect new posts.
[url=http://www.bootscheapsalejp.com/]UGG ???[/url]
<a href="http://www.bootscheapsalejp.com/" title="UGG ???">UGG ???</a>

Posted by anonymous - mulberry at Thursday, November 22, 2012 9:50 PM

Excellent read, I just passed this onto a colleague who was doing some research on that. And he actually bought me lunch since I found it for him smile Therefore let me rephrase that: Thank you for lunch! "Any man would be forsworn to gain a kingdom." by Roger Zelazny.
[url=http://www.bagmulberryuk.co.uk]mulberry[/url]
<a href="http://www.bagmulberryuk.co.uk" title="mulberry">mulberry</a>

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


Vantasy World Copyright 2011 - 2023. Vantasy World is a project developed by Vantasy Online. Privacy Policy.