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:

    The human mind is capable of excitement without the application of gross and violent stimulants; and he must have a very faint perception of its beauty and dignity who does not know this.
    William Wordsworth (1770–1850)

    Children’s view of the world and their capacity to understand keep expanding as they mature, and they need to ask the same questions over and over, fitting the information into their new level of understanding.
    Joanna Cole (20th century)

    The principle goal of education in the schools should be creating men and women who are capable of doing new things, not simply repeating what other generations have done; men and women who are creative, inventive and discoverers, who can be critical and verify, and not accept, everything they are offered.
    Jean Piaget (1896–1980)

    I have a new method of poetry. All you got to do is look over your notebooks ... or lay down on a couch, and think of anything that comes into your head, especially the miseries.... Then arrange in lines of two, three or four words each, don’t bother about sentences, in sections of two, three or four lines each.
    Allen Ginsberg (b. 1926)