Multithreading Problems

Reference :
http://www.ddj.com/184404032;jsessionid=1BXKTQUOD2BDGQSNDLOSKHSCJUNN2JVN?_requestid=468053

Multithreading Problems
The most obvious problem with multithreaded applications is an access violation. An access violation occurs when two or more threads attempt to access the same memory at the same time or when shared memory has been released or resized by one thread without informing the other thread.

Another access violation is caused when the MasterThread tries to access a variable in InputThread that no longer exists , which causes a GPF. This problem is common in C++ programs, where complex structures may be destructed automatically when the thread exits.

A second common problem is deadlock. One way a deadlock can occur is when Thread1 locks ResourceA while Thread2 locks ResourceB. Then, Thread1 attempts to lock ResourceB and waits patiently (keeps trying) until ResourceB is available. Meanwhile, Thread2 attempts to lock ResourceA and waits patiently for ResourceA to be made available. If both threads typically lock one, then the other in quick succession, the bug may rarely show up, and the problem may be blamed on something else, such as hardware or the operating system. You can imagine how this problem becomes complicated with a simulation program where, say, 50 threads are running in parallel.

A second source of deadlock occurs when one thread is waiting for a flag to be set using a blocking call from the Win32 API, such as WaitForSingleObject, but the thread that was supposed to set the flag no longer exists. The thread using the WaitForSingleObject call will wait forever.

A third source of deadlock is when multiple threads simply attempt to lock the same resource at the same time. Nowadays, the operating system or the DBMS are designed to handle this situation. However, we remember some fun times back in DOS days when two threads or two processes both tried printing to the printer at the same time.