Contents
The lockfree package contains lock-free data structures generally meant to be used in realtime or high-performance multithreaded systems.
Currently lockfree contains 2 data structures, FreeList and ObjectPool.
FreeList
The FreeList class provides a fixed number of fixed-size blocks that you can allocate and free from multiple threads. FreeList is lock-free but not wait-free.
Example
ObjectPool<T>
While the FreeList class simply provides allocation of specific-sized blocks of memory, the ObjectPool class builds on top of that to provide allocation of specific object types, initialized using a template object. It also allows you to allocate either boost::shared_ptrs or bare pointers, depending on your needs. ObjectPool is lock-free but not wait-free.
Example
1 struct Foo
2 {
3 std::vector<uint32_t> bar;
4 };
5
6 // Create a template object. All objects in the pool will start initialized to this value.
7 // In realtime systems, for example, this lets you preallocate any vectors/strings/etc.
8 Foo template_object;
9 template_object.bar.resize(100);
10
11 // Construct a pool of 5 objects
12 ObjectPool<Foo> pool(5, template_object);
13
14 boost::shared_ptr<Foo> inst1 = pool.allocate();
15 if (inst1)
16 {
17 ...
18 }
19
20 Foo* inst2 = pool.allocateBare();
21 if (inst2)
22 {
23 ...
24 pool.freeBare(inst2);
25 }