Coder Social home page Coder Social logo

lockfreequeue's Introduction

lockfreequeue

介绍

使用C++11原子操作实现的高性能自旋锁队列

软件架构

安装教程

直接使用

使用说明

#include <thread>
#include <atomic>
#include <string>
#include <iostream>
#include <sstream>
#include <chrono>
#include <stdlib.h>
#include "common_lockfree_queue.h"

/**
* the item in the lock-free queue
*/
class TestEntity
{
public:
	TestEntity(int id_p = 0, int value_p = 0)
	{
		this->id = id_p;
		this->value = value_p;
	}

	void display()
	{
		std::cout << "id = " << id << " value = " << value << std::endl;
	}

private:
	int id;
	int value;
};

LockFreeQueue<TestEntity> queue(4096);

#define COUNT (1000 * 1000)

void produce()
{
	auto beginTime = std::chrono::high_resolution_clock::now();
	unsigned int i = 0;
	while (i < COUNT)
	{
		if (queue.push(TestEntity(i, i)))
		{
			i++;
		}
	}
	auto endTime = std::chrono::high_resolution_clock::now();
	auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - beginTime);

	std::cout << "Producer thread id:[" << std::this_thread::get_id() << "], IO:[" << COUNT * sizeof(TestEntity) * 1.0 / (elapsedTime.count() * 1024 * 1024) * 1000
		<< " MB/s], messages:[" << COUNT * 1.0 / elapsedTime.count() * 1000 << " /s], elapsed:[" << elapsedTime.count()*1.0 / 1000 << "s], item count:[" << COUNT << "]" << std::endl;
}


std::atomic<unsigned int> total_consume(0);

void consume()
{
	TestEntity test;
	auto beginTime = std::chrono::high_resolution_clock::now();
	unsigned int i = 0;
	while (total_consume < 2 * COUNT)
	{
		if (queue.pop(test))
		{
		//	test.display();
			i++;
			total_consume++;
		}
	}
	auto endTime = std::chrono::high_resolution_clock::now();
	auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - beginTime);

	std::cout << "Consumer thread id:[" << std::this_thread::get_id() << "], IO:[" << i * sizeof(TestEntity) * 1.0 / (elapsedTime.count() * 1024 * 1024) * 1000
		<< " MB/s], messages:[" << i * 1.0 / elapsedTime.count() * 1000 << " /s], elapsed:[" << elapsedTime.count()*1.0 / 1000 << "s], item count:[" << i << "]" << std::endl;
}

int main(int argc, char const *argv[])
{
	(void)argc;
	(void)argv;

	std::thread producer1(produce);
	std::thread producer2(produce);
	std::thread consumer1(consume);
	std::thread consumer2(consume);

	producer1.join();
	producer2.join();
	consumer1.join();
	consumer2.join();

	std::cin.get();

	return 0;
}

执行结果

Producer thread id:[140530954135296], IO:[5.79741 MB/s], messages:[759878 /s], elapsed:[1.316s], item count:[1000000]
Producer thread id:[140530962528000], IO:[4.97353 MB/s], messages:[651890 /s], elapsed:[1.534s], item count:[1000000]
Consumer thread id:[140530937349888], IO:[4.74014 MB/s], messages:[621300 /s], elapsed:[1.502s], item count:[933192]
Consumer thread id:[140530945742592], IO:[5.2817 MB/s], messages:[692283 /s], elapsed:[1.541s], item count:[1066808]

lockfreequeue's People

Contributors

videoaudiolijian avatar

Watchers

Li Jian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.