C ++ boost library lock-free queue multi-thread parallel testing and compilation methods
I read the source code about the unlock queue of the Boost library on the network, but there is no compilation method. After testing, the methods for compiling the boost library in ubuntu 14.04 are determined.
Free-lock is an important technology for implementing high-performance multi-thread concurrent programming.
As a boost library for C ++ 11 STL reference implementation, it not only supports the 11 standard, but also implements a lot of extensions to master its usage methods, which is especially important for improving the code quality.
Taking its multi-thread parallel lock-free queue as an example, combined with code and instructions, this article demonstrates the use and compilation methods of the lock-free boost library.
The code and description are as follows:
// Source: boost_queue.cpp // Objective: To test the use of boost lock-free queue // Operating System: ubuntu 14.04 // install the boost library command: sudo apt-get install libboost-all-dev // pubdate: The current boost-dev version is 1.54 // compile the command: g ++ boost_queue.cpp-lboost_thread-lboost_system // boost include location:/usr/include/boost // boost lib location: ls/usr/lib/x86_64-linux-gnu/| grep 'boost '# include
# Include
# Include
# Include
Using namespace std; // production quantity boost: atomic_int producer_count (0); // consumption quantity boost: atomic_int consumer_count (0); // queue boost: lockfree: queue
Queue (512); // number of iterations const int iterations = 1000000; // number of production threads const int producer_thread_count = 4; // Number of consumption threads const int consumer_thread_count = 2; // Production Function void producer (void) {for (int I = 0; I! = Iterations; ++ I) {// atomic count ---- if multiple threads do not count, int value = ++ producer_count; cout <"*"; // observe the production type: pure production or consumption at the same time // If the queue is not entered, the while (! Queue. push (value) ;}// indicates whether the production is complete. boost: atomic
Done (false); // Consumption Function void consumer (void) {int value; // when production is not completed, the while (! Done) {// The while (queue. pop (value) {cout <". "; // observe the consumption type: pure consumption or simultaneous consumption + + consumer_count ;}// if the production is complete, consume while (queue. pop (value) ++ consumer_count;} int main (int argc, char * argv []) {cout <"boost: lockfree: queue is"; if (! Queue. is_lock_free () cout <"not"; cout <"lockfree" <endl; // thread group manager boost: thread_group producer_threads, consumer_threads; // create the producer thread for (int I = 0; I! = Producer_thread_count; ++ I) producer_threads.create_thread (producer); // create a consumer thread for (int I = 0; I! = Consumer_thread_count; ++ I) consumer_threads.create_thread (consumer); // wait for the producer to finish producing producer_threads.join_all (); // The consumption flag done = true; // The main thread sets the done mark cout <"done" <endl; // output to observe the execution sequence of the main thread and sub-threads // wait for the consumer to end consumer_threads.join_all (); // The supply is oversupply because the number of consumers is smaller than the number of producers, subsequent Stages are pure consumption periods // output production and consumption quantities cout <"produced" <producer_count <"objects. "<endl; cout <" consumed "<consumer_count <" objects. "<endl; return 0 ;}