1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
// just a thread safe queue
// actually using std::vector instead of std::queue to limit overheads
// see http://stackoverflow.com/questions/14784551/c-stl-queue-memory-usage-compared-to-vector
// adapted from:
// https://raw.githubusercontent.com/cameron314/concurrentqueue/master/benchmarks/stdqueue.h
// ©2014 Cameron Desrochers.
#pragma once
#include <vector>
// Simple wrapper around std::queue (not thread safe) - RC: made it thread safe
template<typename T>
class LockStdVector
{
public:
LockStdVector()
{
nb_items = 0;
}
template<typename U>
inline bool enqueue(U&& item)
{
std::lock_guard<std::mutex> guard(mutex);
unsigned long size = v.size();
v.push_back(std::forward<U>(item));
nb_items ++;
return true;
}
inline bool try_dequeue(T& item)
{
std::lock_guard<std::mutex> guard(mutex);
if (nb_items == 0) {
return false;
}
item = std::move(v.back());
v.pop_back();
nb_items--;
return true;
}
unsigned long size_approx()
{
return v.size();
}
unsigned long overhead_per_element()
{
return 0 ;
}
private:
std::vector<T> v;
mutable std::mutex mutex;
unsigned long nb_items;
};
|