C h a pter 1 1 : C o n c u r re n cy H a z a r d s in .NET

Creation USS Code 128 in .NET C h a pter 1 1 : C o n c u r re n cy H a z a r d s
C h a pter 1 1 : C o n c u r re n cy H a z a r d s
Code 128A Drawer In Visual Studio .NET
Using Barcode creator for VS .NET Control to generate, create ANSI/AIM Code 128 image in VS .NET applications.
state protected b y it just before being killed for shutdown, the shutdown thread t2 is permitted to freely acquire critical section CSl too, even though it was found as being officially owned by t1 This means any code running during shutdown in pre-Vista ass has to tolerate corrupt state that may have been left behind This is an open-ended requirement that is difficult to achieve, impossible to verify, and many applications get it wrong It's especially difficult if you write reusable library code that somebody else calls during shutdown-maybe they are unaware it uses locks internally-but under rare circumstances, crashes the shutdown process The multithreaded CRT uses locks internally for mem ory allocation and deletion, for instance, and is actually subject to these issues (because it uses locks to protect the free I used lists) It's not even safe to allocate memory during shutdown Other services are apt to suffer from similar problems Waiting on a mutex that was orphaned during shutdown will give you a
Code 128 Code Set A Scanner In .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
WAIT_ABANDON E D return value This at least allows you to detect that a mutex
Barcode Maker In .NET Framework
Using Barcode creator for .NET Control to generate, create bar code image in .NET applications.
was orphaned and react accordingly by validating data, skipping a step in the shutdown cleanup, and so forth Neither weakening nor abandonment apply to other kernel synchronization objects, such as events and semaphores, so you generally can't rely on state invariants associated with them to hold dur ing shutdown either Generally speaking, if you use any sort of cross-thread synchronization in your Dl lMa i n method, you are inviting trouble and long hours of debugging These callbacks must run under the protection of the OS loader lock, which always demands extreme care and thoughtfulness Because of the serious problems this can cause, which often lead to shut down crashes, behavior has changed in Windows Vista Instead of weak ening the locks and permitting threads to observe corrupt state, Windows Vista will immediately terminate the process (via Termi n at e P roc e s s ) when an attempt to acquire an orphaned lock is made on the shutdown thread Although this can lead to some shutdown logic being skipped (which can itself cause problems), all critical data should have been persisted and machine-wide state cleaned up at the application level before the call to E x it P ro c e s s ever occurred Any occurrence of termination during shut down like this is a bug in some code running in the process The challenge is figuring out in which code that bug lives
Barcode Decoder In VS .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET applications.
Corre c t n e s s H a z a r d s
Code 128 Code Set B Printer In C#.NET
Using Barcode generator for VS .NET Control to generate, create Code 128 image in .NET framework applications.
Slim reader/ writer lock ( S R W Lo c k ) acquisitions are inconsistent with everything said above They are not shutdown aware and, hence, trying to acquire an orphaned S RW L o c k on the shutdown thread will cause a hang This might sound bad, but remember, if a lock can be orphaned leading up to a shutdown, there is a bug in the software somewhere Instead of data corruption, you at least have the opportunity to get a Windows Error Reporting hang entry Let's turn to a sample VC++ program that demonstrates this behavior You wouldn't write code this way; it's been specifically crafted to illustrate the orphaning problem First we create a DLL to hold all of the interesting code in its D l l Ma i n : we initialize a C R I T I CA L_S E C T I ON, a mutex, and, on Windows Vista, a S RW L o c k during D L L_PROC E S S_ATTACH, and attempt to acquire them during D L L_PROC E S S_DE TACH We define an exported function, GetAn d B loc k, from our DLL that acquires these synchronization objects and sleeps for a long time with them held This will be called just before we ini tiate the shutdown process from a separate thread, causing all of the locks to become orphaned We also define a function I g n o r eC r it i c a l S e c t i o n , which suppresses critical section acquisition on the shutdown code path (to avoid shutdown in the middle of our test on Vista) This sample code will work on both Windows Vista and older OSs, despite S RW L o c ks not existing, based on whether _WI N 3 2_WINNT is defined at compile time
Draw Code 128 Code Set B In .NET
Using Barcode maker for ASP.NET Control to generate, create Code-128 image in ASP.NET applications.
# i n c lude < st d io h > I I Uncomment when on V i s t a ( o r p a s s it v i a I D on t h e cmd - l i ne ) : II #define _WI N 3 2_WINNT axa6aa # i n c l u d e <windows h > C R I TICAL_S ECTION g_C S j BOOl g_ignoreC s j HANDLE g_mut e x j
Code 128 Code Set A Maker In VB.NET
Using Barcode creation for .NET framework Control to generate, create Code 128 Code Set C image in VS .NET applications.
S RWlOCK gJwl j #e ndif I I C a l l e d d u ring p roc e s s i n it i a l i zation and s h u tdown BOOl WINAPI D I IMa i n ( HINSTANC E h i n s tD l l , DWORD fdwRea son , l PVOI D I p R e s e rved ) {
UCC - 12 Printer In VS .NET
Using Barcode generation for VS .NET Control to generate, create GS1 - 12 image in .NET framework applications.
Barcode Maker In Visual Studio .NET
Using Barcode creator for .NET Control to generate, create bar code image in .NET framework applications.
Print EAN / UCC - 13 In .NET
Using Barcode creation for .NET framework Control to generate, create EAN 13 image in .NET framework applications.
EAN 128 Generation In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create GS1 128 image in ASP.NET applications.
Recognizing Barcode In Visual Studio .NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
Bar Code Generator In Visual C#
Using Barcode creation for VS .NET Control to generate, create barcode image in .NET framework applications.
Paint Bar Code In Visual Basic .NET
Using Barcode generation for .NET framework Control to generate, create bar code image in Visual Studio .NET applications.