[英]Implementation of a simple unit test for lock-free queue
我正在嘗試比較不同的無鎖隊列的性能,因此,我想創建一個單元測試-包括將用戶定義的預構建對象與隊列進行推送/彈出。 因此,我想問您幾個問題:-1)如何以簡單的方式創建預建對象。 像我那樣創建數組是否可以達到目的。 2)我收到一個錯誤“拋出'std :: system_error'what()實例后終止調用:無效參數終止(核心轉儲)”。
提前感謝。
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <chrono>
#include <iostream>
#include <ctime>
#include <atomic>
#include <thread>
#include <boost/lockfree/queue.hpp>
using namespace std;
const long NUM_DATA = 10;
const int NUM_PROD_THREAD = 2;
const int NUM_CONSUM_THREAD = 2;
const long NUM_ITEM = 1000000;
class Data
{
public:
Data(){}
void dataPrint() {cout << "Hello";}
private:
long i;
double j;
};
Data *DataArray = new Data[NUM_DATA];
boost::lockfree::queue<Data*> BoostQueue(1000);
struct Producer
{
void operator()()
{
for(long i=0; i<1000000; i++)
BoostQueue.push( DataArray );
}
};
struct Consumer
{
Data *pData;
void operator()()
{
while ( BoostQueue.pop( pData ) ) ;
}
};
int main(int argc, char** argv)
{
std::thread thrd [NUM_PROD_THREAD + NUM_CONSUM_THREAD];
std::chrono::duration<double> elapsed_seconds;
auto start = std::chrono::high_resolution_clock::now();
for ( int i = 0; i < NUM_PROD_THREAD; i++ )
{
thrd[i] = std::thread{ Producer() };
}
for ( int i = 0; i < NUM_CONSUM_THREAD; i++ )
{
thrd[NUM_PROD_THREAD+i] = std::thread{Consumer()};
}
for ( int i = 0; i < NUM_CONSUM_THREAD; i++ )
{
thrd[i].join();
}
auto end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end - start;
std::cout << "Enqueue and Dequeue 1 million item in:" << elapsed_seconds.count() << std::endl;
for ( int i = 0; i < NUM_PROD_THREAD; i++ )
{
thrd[i].join();
}
return 0;
}
只是說明如何使用Data
元素的標桿,雖然這確實增加了一個cout
的測量時間是不理想,但可能不是顯著要么內。
class Data
{
public:
Data(long i) : i_(i) {}
void dataPrint() {cout << "Hello";}
private:
long i_;
double j;
};
Data* dataArray[1000000];
for (int i = 0; i < NUM_DATA; ++i) dataArray[i] = new Data(i);
boost::lockfree::queue<Data*> BoostQueue(1000);
struct Producer
{
void operator()()
{
for(long i=0; i<1000000; i++)
BoostQueue.push( dataArray[i] );
}
};
struct Consumer
{
Data *pData;
long result_;
void operator()()
{
result_ = 0;
while ( BoostQueue.pop( pData ) )
result_ += pData->i_;
std::cout << result_ << '\n';
}
};
int main(int argc, char** argv)
{
std::thread thrd [NUM_PROD_THREAD + NUM_CONSUM_THREAD];
std::chrono::duration<double> elapsed_seconds;
auto start = std::chrono::high_resolution_clock::now();
for ( int i = 0; i < NUM_PROD_THREAD; i++ )
thrd[i] = std::thread{ Producer() };
for ( int i = 0; i < NUM_CONSUM_THREAD; i++ )
thrd[NUM_PROD_THREAD+i] = std::thread{Consumer()};
for ( int i = 0; i < NUM_CONSUM_THREAD; i++ )
thrd[NUM_PROD_THREAD+i].join();
auto end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end - start;
std::cout << "Enqueue and Dequeue 1 million item in:"
<< elapsed_seconds.count() << std::endl;
for ( int i = 0; i < NUM_PROD_THREAD; i++ )
thrd[i].join();
for (int i = 0; i < 1000000; ++i)
delete dataArray[i];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.