Synchronization Scenarios in Software

Printing QR Code in Software Synchronization Scenarios
Synchronization Scenarios
QR Encoder In Visual C#
Using Barcode maker for .NET framework Control to generate, create Quick Response Code image in Visual Studio .NET applications.
TX T5 T4
Printing QR Code In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
T3 T2 Critical Section Ownership Transfer
Drawing QR Code 2d Barcode In .NET
Using Barcode encoder for VS .NET Control to generate, create QR-Code image in Visual Studio .NET applications.
SYNCHRONIZATION
Print QR-Code In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create QR Code 2d barcode image in .NET applications.
EnterCriticalSection LeaveCriticalSection
GS1 - 13 Recognizer In .NET Framework
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
g_refCount++;
Create USS-128 In .NET
Using Barcode creation for .NET Control to generate, create UCC - 12 image in .NET framework applications.
Time
Print Barcode In VS .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create barcode image in .NET framework applications.
Figure 104 The problem being illustrated the incoming rate of threads trying to acquire the critical section is larger than the amount of time required to enter, process, and leave the critical section (including the work required to transfer ownership) is known as a lock convoy Lock convoys can dramatically reduce the performance of an application due to the excessive amount of work required to manage a fair critical section How does one recognize if a lock convoy problem is indeed occurring At a high level, there are a couple of symptoms that are worth looking out for:
Data Matrix Encoder In C#
Using Barcode drawer for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET applications.
Excessive context switching Remember that for a critical section to fairly transfer ownership to another critical section, it must wake up that new thread The act of waking up a thread might result in a context switch Performance degradation on the code path that includes the locking scheme
Data Matrix Encoder In Visual Basic .NET
Using Barcode printer for .NET framework Control to generate, create Data Matrix image in Visual Studio .NET applications.
If you suspect that a lock convoy might be an issue in your application, the next step is to take a close look at the application using the debuggers and convince yourself that s what s going on Listing 1017 shows a sample application that suffers from the lock convoy problem
Scan Code 128C In .NET Framework
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
10
Bar Code Generation In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create bar code image in ASP.NET applications.
Synchronization
Code 39 Generation In Java
Using Barcode drawer for Java Control to generate, create Code 39 image in Java applications.
Listing 1017
Painting Barcode In Java
Using Barcode printer for Java Control to generate, create bar code image in Java applications.
#include <windowsh> #include <stdioh> CRITICAL_SECTION g_cs; DWORD g_refCount; DWORD WINAPI ThreadProc( LPVOID lpParam ) { while(true) { EnterCriticalSection(&g_cs); g_refCount++; LeaveCriticalSection(&g_cs); } return 1; } void __cdecl main ( ) { DWORD dwId=0; HANDLE hThread[MAXIMUM_WAIT_OBJECTS]; DWORD dwRet=0; g_refCount=0; InitializeCriticalSection(&g_cs); for(int i=0; i<MAXIMUM_WAIT_OBJECTS; i++) { hThread[i] = CreateThread(NULL, 0, ThreadProc, NULL, 0, &dwId); if(!hThread[i]) { for(int j=0;j<i;j++) { CloseHandle(hThread[j]); } return; } } WaitForMultipleObjects(MAXIMUM_WAIT_OBJECTS, hThread, TRUE, INFINITE); for(int i=0; i<MAXIMUM_WAIT_OBJECTS; i++) { CloseHandle(hThread[i]); } }
Making Code 3 Of 9 In Visual Basic .NET
Using Barcode encoder for .NET Control to generate, create Code 39 image in .NET framework applications.
Synchronization Scenarios
Data Matrix 2d Barcode Encoder In .NET
Using Barcode encoder for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications.
The source code and binary for Listing 1017 can be found in the following folders: Source code: C:\AWD\10\LockConvoy Binary: C:\AWDBIN\WinXPx86chk\10lockconvexe The code in Listing 1017 is fairly trivial It creates a number of threads, where each thread sits in a tight loop increasing a reference count variable The reference count variable increment is protected by a critical section If we run this application under the debugger, we see that it contains a large number of threads most of which are waiting to acquire the critical section protecting the reference counter Note that you have to run this application on a Windows version prior to Windows 2003 Server SP1
Generating Bar Code In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create bar code image in .NET framework applications.
61 Id: a48a28 Suspend: 1 Teb: 7ff79000 Unfrozen ChildEBP RetAddr Args to Child 0128ff14 7c90e9c0 7c91901b 0000073c 00000000 ntdll!KiFastSystemCallRet 0128ff18 7c91901b 0000073c 00000000 7c97c140 ntdll!NtWaitForSingleObject+0xc 0128ffa0 7c90104b 00002008 01001169 01002008 ntdll!RtlpWaitForCriticalSection+0x132 0128ffa8 01001169 01002008 0128ffec 7c80b683 ntdll!RtlEnterCriticalSection+0x46 0128ffb4 7c80b683 00000000 7c910833 00000000 10LockConv!ThreadProc+0x19 0128ffec 00000000 01001150 00000000 00000000 kernel32!BaseThreadStart+0x37
Paint Data Matrix In .NET
Using Barcode generator for VS .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
10 SYNCHRONIZATION
Printing Code 128 In VS .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Code 128 image in .NET framework applications.
So far, so good We certainly do expect a large number of threads to be waiting on this very contentious lock Now, let s try to find out which thread owns the critical section Remember, the first parameter to RtlEnterCriticalSection is the address to the critical section in question
Generate UCC - 12 In VS .NET
Using Barcode generation for Visual Studio .NET Control to generate, create UPC Symbol image in Visual Studio .NET applications.
0:065> !cs 01002008 --------------------Critical section = 0x01002008 (10LockConv!g_cs+0x0) DebugInfo = 0x7c97c8c0 LOCKED LockCount = 0x1C OwningThread = 0x00000000 RecursionCount = 0x0 LockSemaphore = 0x73C SpinCount = 0x00000000
EAN 128 Drawer In Java
Using Barcode drawer for Java Control to generate, create EAN 128 image in Java applications.
The LockCount field seems correct (0x1C) and indicates that the critical section is locked and has a large number of threads waiting to acquire it The OwningThread field, on the other hand, seems wrong It implies that a thread with a thread ID of 0 is holding the critical section A thread with a thread ID of 0 will never exist in a process What is going on What you are seeing is, in essence, a telltale sign that a lock convoy is occurring Remember that in a lock convoy situation, the owning thread passes ownership of