C++ boost::lockfree::queue














































C++ boost::lockfree::queue



Description:
The boost :: lockfree concept is concurrent data structure (non-blocking structure) which is implemented with thread.
The data structure is lockfree when some concurrent operations are guaranteed to be finished in a finite number of 
steps.But using lockfree program is difficult task in practical application .there are some restrictions ,the relevant aspects for the implementation of boost::lockfree are number of producer and consumer .apart from lockfree there 
are other two concurrent data structure which are wait-free and obstruction free
explanation:

There are single or multiple producers and single or mulitple consumers.

Disadvantage : it cant be used in every circumstances it is risk taking .therefore for most of the use case it is not the best                                          choice

Advantage: Lockfree data structure will be a better choice in order to optimize the latency of a system or to avoid 
                        priority inversion which is necessary in real time application  

header files:
1. <boost/lockfree/queue> :   for a lockfree multi-producer/ multi-consumer queue

2.<boost/lockfree/stack>: for a lockfree multi-producer/ multi-consumer stack

3.<boost/lockfree/spsc_queue> : a wait free single- produce/single consumer queue .it is also known as ring buffer

 Data structure boost parameters :

1.boost::lockfree::fixedsize  The internal nodes are stored inside an array and they are addressed by array indexing. This                                                          limits the possible size of the queue to the number of elements that can be addressed by the                                                                  index   type , but on platforms that lack double-width compare-and-exchange instructions, this is                                                         the best way to achieve lock-freedom

2.boost::lockfree::capacity : sets the capacity of a data structure at compile -time

3.boost::lockfree::allocator :Defines the allocator. boost.lockfree supports stateful allocator and is compatible                                                                              with Boost. Interprocess allocators

Sources or reasons for blocking behavior:
 Atomic operations: some architectures do not provide atomic operations in natively in hardware there are also known as                                                 spinlocks
Memory allocations: allocating memory from operating system is not lock free .it uses a memory pool to allocate 
                                       the internal node ,if the memory is pool is exhausted then the new node is taken from operating                                                           system,then it create such situations
Exception handling: exception handling does not encourage the real time behavior so it is not encouraged with lockfree

Constructor:

queue(void): it constructs the queue

Member functions:

bool is_lock_free(void) const: it only checks the queue head or tail nodes and the freelist can be modified
                                                                   in a lockfree manner ,it returns true if implementation is lockfree
bool empty(void) const: to check that queue is empty or not ,it returns true is queue is empty

bool push(T const & t): it pushes the object t in the queue

bool pop(T & ret) : it pops the object t from the queue

Program:
#include <boost/thread/thread.hpp>
#include <boost/lockfree/queue.hpp>
#include <iostream>

#include <boost/atomic.hpp>
//atomic operation needed for queue

boost::
atomic_int producer_count(0);
boost::
atomic_int consumer_count(0);

boost::lockfree::
queue<int> queue(128);


void producer(void)
{
for (int i = 0; i != 1000; ++i) {
int value = ++producer_count;
while (!queue.push(value))
;
}
}
boost::atomic<
bool> done (false);
void consumer(void)
{
int value;
while (!done) {
while (queue.pop(value))
++consumer_count;
}

while (queue.pop(value))
++consumer_count;
}

int main()
{
using namespace std;
cout << "boost::lockfree::queue is ";
if (!queue.is_lock_free())
cout << "not ";
cout << "lockfree" << endl;
//creating producer and consumer thread
boost::thread_group thread1, thread2;
// we can observe that queue storage limit is 128
// but we are pushing the 1000 values
for (int i = 0; i != 4; ++i)
thread1.create_thread(producer);

for (int i = 0; i != 4; ++i)
thread2.create_thread(consumer);

thread1.join_all();
done =
true;

thread2.join_all();

cout << "produced " << producer_count << " objects." << endl;
cout << "consumed " << consumer_count << " objects." << endl;
}


output:
Real time examples:
Lamport's "Concurrent Reading and Writing"
 -CACM 20(11), 1977, describes a non-blocking buffer 
- limitations on number of concurrent writer
-lots of user-level music software uses lockfree data structures



More Articles of Konada Sunita:

Name Views Likes
C++ boost::accumulator::p_square_cumulative_distribution 672 1
C++ boost::Numeric Conversion::converter<> 864 10
C++ boost::ref 1227 9
C++ boost::accumulator::count 930 2
C++ boost::function::function_base 557 10
C++ boost::accumulator::weighted_mean 889 1
C++ boost::accumulator::non_coherent_weighted_tail_mean 563 1
C++ boost::accumulator::rolling_count rolling_sum rolling_mean rolling_moment 727 2
C++ boost::lockfree::spsc_queue 2202 10
C++ boost::coroutine:: passing data 640 1
C++ boost::accumulator::weighted_tail_quantile 619 1
C++ boost::TokenizerClass 83 2
C++ boost::accumulator::weighted_variance 559 1
C++ boost::accumulator::rolling mean 2595 2
C++ boost::accumulator::median 1417 1
C++ boost::accumulator::extended_p_square_quantile 837 1
C++ boost::accumulator::extended_p_square 648 1
C++ boost::function::functionN 829 10
C++ boost::accumulator::weighted_density 505 1
C++ boost::accumulator::covariance density 780 3
C++ boost::accumulator::weighted_skewness 503 1
C++ boost::format::formatter 654 10
C++ boost::accumulator::skewness 911 1
C++ boost::accumulator::max 700 3
C++ boost::accumulator::variance 992 1
C++ boost::accumulator::min 592 2
C++ boost::accumulator::density 951 1
C++ boost::token_iterator 885 10
c++ program to print root to leaf paths without using recursion 705 10
C++ boost::accumulator::rolling variance 1476 4
C++ boost::accumulator::weighted_sum 544 1
C++ boost::accumulator::weighted_extended_p_square_quantile 605 2
C++ boost::accumulator::sum_kahan 1117 2
C++ boost::accumulator::weighted_moment 469 1
C++ boost::accumulator::error_of 658 2
C++ boost::accumulator::tail 845 2
C++ boost::format 4139 10
C++ boost::accumulator::weighted_p_square_cumulative_distribution 522 1
C++ boost::accumulator::coherent_tail_mean 471 1
C++ boost::accumulator::rolling count 1153 2
C++ boost::accumulator::moment 703 2
C++ boost::lockfree::stack 1207 8
C++ boost::accumulator::sum 821 2
C++ boost::function::bad_function_call() 1096 10
C++ boost::accumulators::Mean Moment Count 1370 2
C++ boost::accumulator::mean 1617 2
C++ boost::accumulator::non_coherent_tail_mean 501 1
C++ boost::accumulator::weighted_extended_p_square 521 1
C++ boost::accumulator::weighted_kurtosis 522 1
C++ boost::accumulator::covariance density error_of_mean 587 9
C++ boost::accumulator::rolling sum 834 2
C++ boost::lockfree::queue 3085 9
C++ boost::accumulator::weighted_p_square_quantile 852 1
C++ boost::accumulator::p_square_quantile 840 1
C++ boost::accumulator::tail_quantile 1201 1
C++ boost::accumulator::rolling moment 598 2
C++ boost::accumulator::min max 1230 3
C++ boost::accumulator::kurtosis 1209 1
C++ boost::coroutine::Pushtype and pulltype 1564 1
C++ boost::accumulator::tail_variate 491 2
C++ boost::accumulator::weighted_covariance 519 2
C++ boost::format::Exception 2202 10
C++ boost::function 3035 10
C++ boost::NumericConversion::bounds<> 677 10
C++ boost::NumericConversion::UDT 853 10

Comments