C++boostライブラリ無ロックキューマルチスレッド並列テストとコンパイル方法
ネットワーク内のBoostライブラリのロックされていないキューに関するソースコードを読みましたが、コンパイル方法がありません.テストの結果,ubuntu 14.04でboostライブラリをコンパイルする方法を決定し,特に記録した.
無ロック(free-lock)は高性能マルチスレッドの同時プログラミングを実現する重要な技術である.
C++11 STLリファレンスとして実装されたboostライブラリは,11規格のみならず,多くの拡張を行い,その使用方法を把握することがコード品質の向上に特に重要である.
そのマルチスレッドパラレルロックなしキューを例に、コードと説明を組み合わせて、ロックなしboostライブラリの使用とコンパイル方法を実証した.
コードと説明は以下の通りです.
無ロック(free-lock)は高性能マルチスレッドの同時プログラミングを実現する重要な技術である.
C++11 STLリファレンスとして実装されたboostライブラリは,11規格のみならず,多くの拡張を行い,その使用方法を把握することがコード品質の向上に特に重要である.
そのマルチスレッドパラレルロックなしキューを例に、コードと説明を組み合わせて、ロックなしboostライブラリの使用とコンパイル方法を実証した.
コードと説明は以下の通りです.
//source: boost_queue.cpp
// : boost
// :ubuntu 14.04
// boost :sudo apt-get install libboost-all-dev
//pubdate: 2015-1-31 boost-dev 1.54
// : g++ boost_queue.cpp -lboost_thread -lboost_system
//boost include : /usr/include/boost
//boost lib : ls /usr/lib/x86_64-linux-gnu/ | grep 'boost'
#include
#include
#include
#include
using namespace std;
//
boost::atomic_int producer_count(0);
//
boost::atomic_int consumer_count(0);
//
boost::lockfree::queue queue(512);
//
const int iterations = 1000000;
//
const int producer_thread_count = 4;
//
const int consumer_thread_count = 2;
//
void producer(void)
{
for (int i = 0; i != iterations; ++i) {
// ————
int value = ++producer_count;
cout << "*"; // :
// ,
while (!queue.push(value)) ;
}
}
//
boost::atomic done (false);
//
void consumer(void)
{
int value;
// ,
while (!done) {
// ,
while (queue.pop(value)) {
cout << "."; // :
++consumer_count;
}
}
// ,
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;
//
boost::thread_group producer_threads, consumer_threads;
//
for (int i = 0; i != producer_thread_count; ++i)
producer_threads.create_thread(producer);
//
for (int i = 0; i != consumer_thread_count; ++i)
consumer_threads.create_thread(consumer);
//
producer_threads.join_all();
//
done = true; // done
cout << "done" << endl; //
//
consumer_threads.join_all(); // , , ,
//
cout << "produced " << producer_count << " objects." << endl;
cout << "consumed " << consumer_count << " objects." << endl;
return 0;
}