Critical Section - Application Level Critical Sections

Application Level Critical Sections

Application-level critical sections reside in the memory range of the process and are usually modifiable by the process itself. This is called a user-space object because the program run by the user (as opposed to the kernel) can modify and interact with the object. However, the functions called may jump to kernel-space code to register the user-space object with the kernel.

Example Code For Critical Sections with POSIX pthread library

/* Sample C/C++, Unix/Linux */ #include /* This is the critical section object (statically allocated). */ static pthread_mutex_t cs_mutex = PTHREAD_MUTEX_INITIALIZER; void f { /* Enter the critical section -- other threads are locked out */ pthread_mutex_lock( &cs_mutex ); /* Do some thread-safe processing! */ /*Leave the critical section -- other threads can now pthread_mutex_lock */ pthread_mutex_unlock( &cs_mutex ); } int main { f; return 0; }

Example Code For Critical Sections with Win32 API

/* Sample C/C++, Windows, link to kernel32.dll */ #include static CRITICAL_SECTION cs; /* This is the critical section object -- once initialized, it cannot be moved in memory */ /* If you program in OOP, declare this as a non-static member in your class */ void f { /* Enter the critical section -- other threads are locked out */ EnterCriticalSection(&cs); /* Do some thread-safe processing! */ /* Leave the critical section -- other threads can now EnterCriticalSection */ LeaveCriticalSection(&cs); } int main { /* Initialize the critical section before entering multi-threaded context. */ InitializeCriticalSection(&cs); f; /* Release system object when all finished -- usually at the end of the cleanup code */ DeleteCriticalSection(&cs); return 0; }

Note that on Windows NT (not 9x/ME), the function TryEnterCriticalSection can be used to attempt to enter the critical section. This function returns immediately so that the thread can do other things if it fails to enter the critical section (usually due to another thread having locked it). With the pthreads library, the equivalent function is pthread_mutex_trylock. Note that the use of a CriticalSection is not the same as a Win32 Mutex, which is an object used for inter-process synchronization. A Win32 CriticalSection is for intra-process synchronization (and is much faster regarding lock times), however it cannot be shared across processes.

Read more about this topic:  Critical Section

Famous quotes containing the words application, level, critical and/or sections:

    Most people, no doubt, when they espouse human rights, make their own mental reservations about the proper application of the word “human.”
    Suzanne Lafollette (1893–1983)

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)

    Probably more than youngsters at any age, early adolescents expect the adults they care about to demonstrate the virtues they want demonstrated. They also tend to expect adults they admire to be absolutely perfect. When adults disappoint them, they can be critical and intolerant.
    —The Lions Clubs International and the Quest Nation. The Surprising Years, I, ch.4 (1985)

    ... many of the things which we deplore, the prevalence of tuberculosis, the mounting record of crime in certain sections of the country, are not due just to lack of education and to physical differences, but are due in great part to the basic fact of segregation which we have set up in this country and which warps and twists the lives not only of our Negro population, but sometimes of foreign born or even of religious groups.
    Eleanor Roosevelt (1884–1962)