I am trying to compare performance of different lock-free queues, therefore, I want to create a unit test - which includes pushing/poping user-defined pre-built objects to and from the queue. Therefore, I want to ask you couple of questions:- 1) How to create pre-built objects in a simple manner. Does creating an array like I did would fulfill the purpose. 2) I am getting an error "terminate called after throwing an instance of 'std::system_error' what(): Invalid argument Aborted (core dumped)".
Thanx in advance.
#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;
}