SafeQueue类继承与信号量mutex(用于加锁),nonocopyable

定义如下:

template <typename T>
struct SafeQueue : private std::mutex, private noncopyable {
static const int wait_infinite = std::numeric_limits<int>::max(); SafeQueue(size_t capacity = ) : capacity_(capacity), exit_(false) {}
bool push(T&& v)
T pop_wait(int waitMs = wait_infinite);
bool pop_wait(T* v, int waitMs = wait_infinite); size_t size();
void exit();
bool exited() { return exit_; } private:
std::list<T> items_;
std::condition_variable ready_;
size_t capacity_;
std::atomic<bool> exit_;
void wait_ready(std::unique_lock<std::mutex>& lk, int waitMs);
};

该类可以安全的添加和删除任务,类内部使用容器list来存储具体的任务,具有退出状态:exit_,取出任务时可以设定超时时间。

其中Task的定义为:typedef std::function<void()> Task;  Task为返回值为空的函数对象

SafeQueue的具体实现如下:

template <typename T>
size_t SafeQueue<T>::size() {
std::lock_guard(std::mutex) lk(*this);
return items_.size();
} template <typename T>
void SafeQueue<T>::exit() {
exit_ = true;
std::lock_guard<std::mutex> lk(*this);
ready_.notify_all();
} template <typename T>
bool SafeQueue<T>::push(T&& v) {
std::lock_guard<std::mutex> lk(*this);
if (exit_ || (capacity_ && items_.size() >= capacity_)) {
return false;
} items_.push_back(std::move(v));
ready_.notify_one();
return true;
} template <typename T>
void SafeQueue<T>::wait_ready(std::unique_lock<std::mutex>& lk, int waitMs) {
if (exit_ || !items_.empty()) {
return;
}
if (waitMs == wait_infinite) {
ready_.wait(lk, [this] { return exit_ || !items_.empty(); });
} else if (waitMs > ) {
auto tp = std::chrono::steady_clock::now() + std::chrono::milliseconds(waitMs);
while (ready_.wait_until(lk, tp) != std::cv_status::timeout && items_.empty() && !exit_) {
}
}
} template <typename T>
bool SafeQueue<T>::pop_wait(T* v, int waitMs) {
std::unique_lock<std::mutex> lk(*this);
wait_ready(lk, waitMs);
if (items_.empty()) {
return false;
} *v = std::move(items_.front());
items_.pop_front();
return true;
} template <typename T>
T SafeQueue<T>::pop_wait(int waitMs) {
std::unique_lock<std::mutex> lk(*this);
wait_ready(lk, waitMs);
if (items_.empty()) {
return T();
}
T r = std::move(items_.front());
items_.pop_front();
return r;
}

handy源码阅读(三):SafeQueue类的更多相关文章

  1. 25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment

    25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment 25 BasicUsageEnvironment0基本使用环境基类— ...

  2. 24 UsageEnvironment使用环境抽象基类——Live555源码阅读(三)UsageEnvironment

    24 UsageEnvironment使用环境抽象基类——Live555源码阅读(三)UsageEnvironment 24 UsageEnvironment使用环境抽象基类——Live555源码阅读 ...

  3. 26 BasicUsageEnvironment基本使用环境——Live555源码阅读(三)UsageEnvironment

    26 BasicUsageEnvironment基本使用环境--Live555源码阅读(三)UsageEnvironment 26 BasicUsageEnvironment基本使用环境--Live5 ...

  4. SparkSQL(源码阅读三)

    额,没忍住,想完全了解sparksql,毕竟一直在用嘛,想一次性搞清楚它,所以今天再多看点好了~ 曾几何时,有一个叫做shark的东西,它改了hive的源码...突然有一天,spark Sql突然出现 ...

  5. handy源码阅读(二):EventsImp类

    EventsImp用于完成事件的处理. class EventsImp { EventBase* base_; PollerBase* poller_; std::atomic<bool> ...

  6. handy源码阅读(六):tcp类

    首先是tcpconn和tcpserver类: struct TcpConn : public std::enable_shared_from_this<TcpConn>, private ...

  7. handy源码阅读(六):udp类

    分为UdpServer类和UdpConn类. struct UdpServer : public std::enable_shared_from_this<UdpServer>, priv ...

  8. handy源码阅读(四):Channel类

    通道,封装了可以进行epoll的一个fd. struct Channel: private noncopyable { Channel(EventBase* base, int fd, int eve ...

  9. handy源码阅读(一):EventBase类

    类EventBase继承于类EventBases,继承于noncopyable.  其中noncopyable是一个去除了拷贝构造和赋值构造的类. noncopyable: class noncopy ...

随机推荐

  1. fedora23帮定键盘系统操作快捷键

    在All settings -> keyboard 主要是以super为主, 然后有 super+ shift+...虽然感觉用 ctrl+super+... 来组合更方便, 但是用 shift ...

  2. MongoDB学习【四】—pymongo操作mongodb数据库

    一.pymongodb的安装 Python 要连接 MongoDB 需要 MongoDB 驱动,这里我们使用 PyMongo 驱动来连接. pip安装 pip 是一个通用的 Python 包管理工具, ...

  3. robot framework :List Variables-List变量及其用法

    [转自:https://blog.csdn.net/yezibang/article/details/52692342] 这一讲我们重点来介绍List Variables-List变量及其用法. 一. ...

  4. Map m=new HashMap()

    Map<String,String> m=new HashMap<String,String>() 等于 HashMap<String,String> hashMa ...

  5. (selenium+python)_UI自动化01_Mac下selenium环境搭建

    前言 Selenium 是一个用于Web网页UI自动化测试的开源框架,可以驱动浏览器模拟用户操作.支持多种平台(Windows.Mac OS.Linux)和多种浏览器(IE.Firefox.Chrom ...

  6. mooc-IDEA postfix--007

    十三.IntelliJ IDEA -postfix 代码中输入: 总结常用的postfix: 1.for  (<=>100.fori) 2.sout (<=>System.ou ...

  7. 20191105 《Spring5高级编程》笔记-第10章

    第10章 使用类型转换和格式化进行验证 在应用程序开发中,数据验证通常与转换和格式化一起被提及.因为数据源的格式很可能与应用程序中所使用的格式不同. 名词缩写: SPI(Service Provide ...

  8. [2019杭电多校第五场][hdu6624]fraction

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6624 题意为求最小的b满足$a*b^{-1}\equiv x(modp)$. 把式子化简一下: $a\ ...

  9. SCUT - 153 - 小马哥和他的山脉 - 线段树

    https://scut.online/p/153 其实不需要用线段树,只关心相邻元素的差,像神仙那样用差分就可以O1维护的. 但是我偏要用. 交之前写的那个,注意没有st本身的线段树只有lazy标记 ...

  10. HTML5-video(播放暂停视频;打开关闭声音;进度条)

    <!DOCTYPE html> <html> <head> <title>HTML5-video(播放暂停视频:打开关闭声音:进度条)</titl ...