test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SerialTaskQueue Class Reference

#include "FWCore/Concurrency/interface/SerialTaskQueue.h"

Detailed Description

Description: Runs only one task from the queue at a time

Usage: A SerialTaskQueue is used to provide thread-safe access to a resource. You create a SerialTaskQueue for the resource. When every you need to perform an operation on the resource, you push a 'task' that does that operation onto the queue. The queue then makes sure to run one and only one task at a time. This guarantees serial access to the resource and therefore thread-safety.

The 'tasks' managed by the SerialTaskQueue are just functor objects who which take no arguments and return no values. The simplest way to create a task is to use a C++11 lambda.

Example: Imagine we have the following data structures.

std::vector<int> values;

On thread 1 we can fill the vector

for(int i=0; i<1000;++i) {
queue.pushAndWait( [&values,i]{ values.push_back(i);} );
}

While on thread 2 we periodically print and stop when the vector is filled

bool stop = false;
while(not stop) {
queue.pushAndWait([&false,&values] {
if( 0 == (values.size() % 100) ) {
std::cout <<values.size()<<std::endl;
}
if(values.size()>999) {
stop = true;
}
});
}