mpmc bounded queue and thread task stolen, simple
来源:互联网 发布:淘宝天天特价活动封面 编辑:程序博客网 时间:2024/05/16 08:58
#include <thread>
#include <iostream>
#include <atomic>
using namespace std;
using mpmcQ = mpmc_bounded_queue<char*>;
mpmcQ q(1024);
std::atomic<bool> empty;
std::atomic<bool> full;
void m_work(void)
{
char* p = new char[rand()%(1024*5)];
//char* p = new char[1024*1024*4];
if(!q.enqueue(move(p)))
{
cout << "Q full, size " << q.approx_size() << endl;
delete[] p;
//full = true;
//empty = false;
}
if(q.approx_size() > (q.size()*2/3))
{
full = true;
empty = false;
}
}
void f_work(void)
{
char* p;
if(q.dequeue(p))
{
delete[] p;
}
else
{
cout << "Q empty, size " << q.approx_size() << endl;
//empty = true;
//full = false;
}
if(q.approx_size() < (q.size()/3))
{
empty = true;
full = false;
}
}
using fn = void (*)(void);
static atomic<fn> g_work[19];
void work(int id)
{
start:
if(empty)
g_work[19] = m_work;
else
g_work[19] = f_work;
if(full)
g_work[0] = f_work;
else
g_work[0] = m_work;
fn f = g_work[id];
f();
this_thread::sleep_for(chrono::milliseconds(5));
goto start;
}
int main()
{
srand((unsigned)time(NULL));
for(auto i = 0; i < 10; i++)
g_work[i] = m_work;
for(auto i = 10; i < 19; i++)
g_work[i] = f_work;
for(auto i = 0; i < 19; i++)
thread(work, i).detach();
thread([]{
start:
this_thread::sleep_for(chrono::seconds(5));
cout << "Q size " << q.approx_size() << endl;
goto start;
}).detach();
char p;
cin >> p;
return 0;
}
/*
A modified version of Bounded MPMC queue by Dmitry Vyukov.
Original code from:
http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue
licensed by Dmitry Vyukov under the terms below:
Simplified BSD license
Copyright (c) 2010-2011 Dmitry Vyukov. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the authors and
should not be interpreted as representing official policies, either expressed or implied, of Dmitry Vyukov.
*/
/*
The code in its current form adds the license below:
Copyright(c) 2015 Gabi Melman.
Distributed under the MIT License (http://opensource.org/licenses/MIT)
*/
/*
port out by lma from spdlog
*/
#pragma once
#include <atomic>
#include <utility>
#include <stdlib.h>
template<typename T>
class mpmc_bounded_queue
{
public:
using item_type = T;
mpmc_bounded_queue(size_t buffer_size)
:max_size_(buffer_size),
buffer_(new cell_t [buffer_size]),
buffer_mask_(buffer_size - 1)
{
//queue size must be power of two
if(!((buffer_size >= 2) && ((buffer_size & (buffer_size - 1)) == 0)))
{
exit(-1);
// you can work around it by kfifo
}
for (size_t i = 0; i != buffer_size; i += 1)
buffer_[i].sequence_.store(i, std::memory_order_relaxed);
enqueue_pos_.store(0, std::memory_order_relaxed);
dequeue_pos_.store(0, std::memory_order_relaxed);
}
~mpmc_bounded_queue()
{
delete [] buffer_;
}
size_t size(void)
{
return max_size_;
}
bool enqueue(T&& data)
{
cell_t* cell;
size_t pos = enqueue_pos_.load(std::memory_order_relaxed);
for (;;)
{
cell = &buffer_[pos & buffer_mask_];
size_t seq = cell->sequence_.load(std::memory_order_acquire);
intptr_t dif = (intptr_t)seq - (intptr_t)pos;
if (dif == 0)
{
if (enqueue_pos_.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
break;
}
else if (dif < 0)
{
return false;
}
else
{
pos = enqueue_pos_.load(std::memory_order_relaxed);
}
}
cell->data_ = std::move(data);
cell->sequence_.store(pos + 1, std::memory_order_release);
return true;
}
bool dequeue(T& data)
{
cell_t* cell;
size_t pos = dequeue_pos_.load(std::memory_order_relaxed);
for (;;)
{
cell = &buffer_[pos & buffer_mask_];
size_t seq =
cell->sequence_.load(std::memory_order_acquire);
intptr_t dif = (intptr_t)seq - (intptr_t)(pos + 1);
if (dif == 0)
{
if (dequeue_pos_.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
break;
}
else if (dif < 0)
return false;
else
pos = dequeue_pos_.load(std::memory_order_relaxed);
}
data = std::move(cell->data_);
cell->sequence_.store(pos + buffer_mask_ + 1, std::memory_order_release);
return true;
}
size_t approx_size()
{
size_t first_pos = dequeue_pos_.load(std::memory_order_relaxed);
size_t last_pos = enqueue_pos_.load(std::memory_order_relaxed);
if (last_pos <= first_pos)
return 0;
auto size = last_pos - first_pos;
return size < max_size_ ? size : max_size_;
}
private:
struct cell_t
{
std::atomic<size_t> sequence_;
T data_;
};
size_t const max_size_;
static size_t const cacheline_size = 64;
typedef char cacheline_pad_t [cacheline_size];
cacheline_pad_t pad0_;
cell_t* const buffer_;
size_t const buffer_mask_;
cacheline_pad_t pad1_;
std::atomic<size_t> enqueue_pos_;
cacheline_pad_t pad2_;
std::atomic<size_t> dequeue_pos_;
cacheline_pad_t pad3_;
mpmc_bounded_queue(mpmc_bounded_queue const&) = delete;
void operator= (mpmc_bounded_queue const&) = delete;
};
- mpmc bounded queue and thread task stolen, simple
- Working with bounded Task Flows
- Simple Queue
- simple version of stack and queue 's ADT
- Dead simple example of using Multiprocessing Queue, Pool and Locking
- Activity, Service,Task, Process and Thread之间的关系
- Activity, Service,Task, Process and Thread之间的关系
- Activity, Service,Task, Process and Thread之间的关系
- Activity, Service,Task, Process and Thread之间的关系
- Activity, Service,Task, Process and Thread之间的关系
- Activity, Service,Task, Process and Thread之间关系简介
- c implementation of a bounded buffer queue using posix threads
- Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms
- Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms
- 1382_A Simple Task
- HDU1339:A Simple Task
- hdu1339-A Simple Task
- CF11D A Simple Task
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- 程序设计的基本目标
- Windbg中查看函数参数
- spring JmsTemplate的sendAndReceive方法
- ElasticSearch里面的路由功能介绍
- mpmc bounded queue and thread task stolen, simple
- 《Cracking the Coding Interview程序员面试金典》-----
- spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案
- 【学习笔记】模块初始化/抢占x互斥与锁/
- java static、final关键字
- 51Nod 1005 大数加法
- JavaScript入门
- QtitanRibbon:Qt类跨平台Ribbon UI组件—介绍
- 剑指offer面试题[10]-二进制中1的个数