Skip to Main Content
Intel Threading Building Blocks
book

Intel Threading Building Blocks

by James Reinders
July 2007
Intermediate to advanced content levelIntermediate to advanced
332 pages
10h 4m
English
O'Reilly Media, Inc.
Content preview from Intel Threading Building Blocks

Mutex Concept

The mutexes and locks here have simple interfaces that are designed for high performance. The interfaces enforce the scoped locking pattern, which is widely used in C++ libraries because:

  • It does not require the programmer to remember to release the lock.

  • It releases the lock if an exception is thrown out of the mutual exclusion region protected by the lock.

There are two parts to the pattern: a mutex and a lock. The constructor of the lock object acquires the lock, and the destructor of the lock object releases the lock. Here’s an example:

	{
	    // Construction of myLock acquires lock on myMutex
	    M::scoped_lock myLock( myMutex );
	    ... actions to be performed while holding the lock ...
	    // Destruction of myLock releases lock on myMutex
	}

If the actions throw an exception, the lock is automatically released as the block is exited.

Table 7-2 summarizes the classes that model the Mutex Concept.

Table 7-2. Mutex Concept

Pseudosignature

Semantics

M ()

Construct unlocked mutex.

~M ()

Destroy unlocked mutex.

M ::scoped_lock( )

Construct lock without acquiring mutex.

M ::scoped_lock(M&)

Construct lock and acquire lock on mutex.

M ::~scoped_lock( )

Release lock (if acquired).

M ::scoped_lock::acquire(M&)

Acquire lock on mutex.

M ::scoped_lock::release( )

Release lock.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Intel® Xeon Phi™ Coprocessor Architecture and Tools: The Guide for Application Developers

Intel® Xeon Phi™ Coprocessor Architecture and Tools: The Guide for Application Developers

Rezaur Rahman

Publisher Resources

ISBN: 9780596514808Errata Page