Figure 7-8. Typical Deadlock in .NET Drawer Data Matrix 2d barcode in .NET Figure 7-8. Typical Deadlock

Figure 7-8. Typical Deadlock using barcode generator for vs .net control to generate, create gs1 datamatrix barcode image in vs .net applications. data matrix Although typically a Data Matrix for .NET two-thread problem, deadlocks can involve dozens of threads in a circle, all waiting for one another. They can involve a single thread that tries to obtain the same (nonrecursive) mutex twice, and they can involve a thread that holds a lock dying while another thread is waiting for it.

Deadlocks can always be avoided simply by using careful programming practices. If you declare a lock hierarchy and always acquire locks in the same order A before B before C, etc. then there is no chance of a deadlock.

When you want to do out-of-order locking, you can use the trylock functions to see whether you can get all the locks you need, and if not, then release them all and try again later (Code Example 7-8). Example 7-8 Locking Mutexes Out of Order. pthread_mutex_lock(& m2); ...

if (EBUSY == pthread_mutex_trylock(&m1)) { pthread_mutex_unlock(&m2); pthread_mutex_lock(&m1); pthread_mutex_lock(&m2); } do_real_work(); /* Got "em both! */. A typical instance o ECC200 for .NET f this out-of-order locking is the Solaris virtual memory system, which must lock access to pages. There is an official hierarchy which says that page 1 must be locked before page 2, etc.

Occasionally, the VM system will lock page 2 and then discover that it also wants page 1. It will then execute a trylock on page 1. If that succeeds, all is well and it proceeds.

If it fails, it releases the lock on page 2 and requests the locks in proper order.[3] This is a simple optimization that saves a bit of time in the normal case and is always correct..

Note that you must r elease lock m2. Just spinning, waiting for m1 to become available, will not work..

Obviously, this kind .NET Data Matrix ECC200 of design is not possible using Java"s synchronized sections. You could extend our Mutex class to behave like this, but the overhead is so large that it is very unlikely to be worth the effort.

. Race Conditions Races are instances gs1 datamatrix barcode for .NET of indeterminacy in otherwise deterministic programs. The result a program will give in a race condition depends upon the luck of the draw which thread happens to run first, which LWP happens to get kicked off its processor by the page daemon, etc.

Race conditions are generally bad things, although there are times when they are acceptable. Certainly, one would be upset if 1414.60/2.

414 came out to be 586 on one run of a program and 586.001 on the next. Most commonly, race conditions come around in programs in which the programmer forgot to write proper locking protection around some shared data or when locks were taken out of order.

Still, it is certainly possible to write code that is perfectly correct, yet suffers from races. Consider Code Example 7-9; if v starts with the value one, the result will either be one or zero, depending upon which thread runs first. Example 7-9 Simplistic Race Condition.

Thread 1 synchronize d (one) { v = v - 1; } Thread 2 synchronized (one) { v = v * 2; }. It is worth noting t Data Matrix barcode for .NET hat some instances of indeterminacy in a program are acceptable. If you write a program that searches for a solution to a chess problem by creating lots of threads to consider lots of different possible moves, you may get different answers depending upon which thread completes first.

As long as you get one good answer ("Checkmate in three!"), you don"t really care if you move your pawn or your rook first.. Recovering from Deadlocks A common question is VS .NET DataMatrix , "What if a thread that is holding a lock dies How can I recover from this " The first answer is, "You can"t." If a thread was holding a lock, it could legitimately have changed portions of the data that the lock protected in ways impossible to repair.

If it was in the midst of changing the balance of your bank account, there is no inherent way for you to know whether or not it had credited the deposit it was working on. This is, of course, a very bad thing. Pthreads makes no provision for this situation.

Only the owner of a mutex can release it, and should that owner die, the mutex will never be released. Period. This is not really a problem for well-written programs.

The only way for a thread to die is for the programmer to write the code that kills it. Thus, the proper answer here is, "Fix your code!" You can, however, build arbitrarily complex "recoverable" locks from the primitives in all the libraries. Using them properly is the trick.

Win32 and UI robust mutexes do allow recovery should the owner thread die. This is nice functionality if you need it, but it makes mutexes more expensive to use when you don"t. In a single-process, multithreaded program, recovering from deadlocks is not too much of an issue.

You have complete control over your threads, and if your process dies, all the threads die with it. In a shared memory, multiple-process program, it is more problematic, as it is possible for one process to die while leaving others running..

It is somewhat reaso nable to consider recovering from a deadlock in the case of a process dying unexpectedly. In other deadlock situations, where threads are waiting for each other, you really shouldn"t be looking at recovery techniques. You should be looking at your coding techniques.

System V-shared semaphores do make provision for recovery, and they may prove to be the solution to your problem. They provide room for a system-maintained "undo" structure, which will be invoked should the owner process die, and they can be reset by any process with permission. They are expensive to use, though, and add complexity to your code.

Both Win32 and UI robust mutexes have built-in "death detection" also, so that your program can find out that the mutex it was waiting for was held by a newly dead thread. Still, just having to undo structures that can reset mutexes does not solve the real problem. The data protected may be inconsistent, and this is what you have to deal with.

It is possible to build arbitrarily complex undo structures for your code, but it is a significant task that should not be undertaken lightly. Database systems do this routinely via two-phase commit strategies, as they have severe restrictions on crash recovery. Essentially, what they do is (1) build a time-stamped structure containing what the database will look like at the completion of the change; (2) save that structure to disk and begin the change; (3) complete the change; (4) update the time stamp on the database; and (5) delete the structure.

A crash at any point in this sequence of events can be recovered from reliably. Java does not have anything similar to these recoverable mutexes, nor does it need them. Java programs are either single process programs (in which case a deadlock is a programming bug) or they use RMI or some other kind of remote method invocation (in which case the RMI package is responsible for dealing with dead processes).

Be very, very careful when dealing with this problem!.
Copyright © . All rights reserved.