Tutorial : Concurrent Containers

Previous : Generic Parallel Algorithms Top : Tutorial Next: Flow Graph

Concurrent Containers

Intel® Threading Building Blocks (Intel® TBB) library provides highly concurrent container classes. These containers can be used with raw Windows* OS or Linux* OS threads, or in conjunction with Intel TBB algorithms or task-based programming.

A concurrent container allows multiple threads to concurrently access and update items in the container. Typical C++ STL containers do not permit concurrent update; attempts to modify them concurrently often result in corrupting the container. STL containers can be wrapped in a mutex to make them safe for concurrent access, by letting only one thread operate on the container at a time, but that approach eliminates concurrency, thus restricting parallel speedup.

Containers provided by Intel® TBB offer a much higher level of concurrency, via one or both of the following methods:

  • Fine-grained locking: Multiple threads operate on the container by locking only those portions they really need to lock. As long as different threads access different portions, they can proceed concurrently.
  • Lock-free techniques: Different threads account and correct for the effects of other interfering threads.

Consider a queue as an example.  The following serial function checks if the queue MySerialQueue is empty and then, if not, it pops and processes an item from the queue.  If this were done in a parallel context, another thread could pop off the front of the queue after this function has checked for emptiness but before it performs the pop, leading to a failure.  

extern std::queue<T> MySerialQueue;
T item;
if( !MySerialQueue.empty() ) {
    item = MySerialQueue.front();
    MySerialQueue.pop_front();
    ... process item...
}

In contrast, the Intel TBB queue provides thread-friendly interfaces to its containers.  Below, a try_pop call is made on the concurrent_queue MyQueue.   This function combines the emptiness test with the pop, preventing the failure.

extern concurrent_queue<T> MyQueue;
T item;
if( MyQueue.try_pop(item) ) {
    ...process item...
}    

There are a number of resources available to learn more about the concurrent containers supported by the Intel® Threading Building Blocks library.

 

Previous : Generic Parallel Algorithms Top : Tutorial Next: Flow Graph