[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
PitfallsinThreads
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 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
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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)
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
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
