[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
PitfallsinThreads
= Pitfalls in Threads =


Why have this topic second when usually its the last thing discussed?

Well I want to give you an idea of what problems can occur from the very beginning, keeping this in the back of your mind is always important when designing a threaded application.

The two most prevalent problems are...

  • Data corruption
  • Application freezes (caused by fixing data corruption)
What causes data corruption?

Data is corrupted in a multithreaded environment when two or more threads try to write to the same piece of data at the same time. it is ok for multiple threads to read from a variable but writing can cause problems.

Atomic variables such as pointers integers etc.. are generally ok as they are written to in one CPU cycle, but when compound objects such as structures are written to the problem occurs. One thread may be in the process of writing information to the structure but be stopped halfway through, as another thread becomes active, the other thread then tries to read from or write to the structure resulting in mixed and possibly invalid data.

To fix this we need to regulate access to the variable so that if a thread is writing to a variable, no other thread can read or write that variable.

(to be continued)

Why does fixing it cause Applications to freeze?

The way to fix the above problem is to prevent multiple threads from gaining access to a variable if another thread is already writing to it

This is known as blocking. in other words when one thread accesses the variable or more accurately a section of code, other threads are stopped or suspended if they try to access the same variable or protected section of code.

Why does this cause freezes ?

If a thread that is blocking other threads, becomes blocked itself by one of the other threads it won't be able to return to release the block.

Ie. the first thread blocks the second thread, which in turn blocks the first thread.

This is called a deadlock. and all processing in those two or more threads stops forever.... or until CTL-ALT-DEL

(to be continued)

Return to Threads

last edited (March 2, 2003) by ravenm, Number of views: 2296, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.