一个c++11自定义的信号量
1.关于
This is from here
But I did some changes.
2. semaphore.h
/**
@ brief : this is from https://stackoverflow.com/questions/4792449/c0x-has-no-semaphores-how-to-synchronize-threads
But, I did some changes
*/
#pragma once
#include <mutex>
#include <condition_variable>
namespace toolkit
{
class semaphore
{
public:
explicit semaphore(unsigned int count_lock = 0);
virtual ~semaphore();
inline void wait();
inline bool try_wait();
inline void notify();
semaphore(const semaphore& instance) = delete;
semaphore& operator = (const semaphore& instande) = delete;
semaphore(const semaphore&& instance) = delete;
semaphore& operator = (const semaphore&& instande) = delete;
private:
unsigned int _cnt_lock = 0;
std::mutex _mtx;
std::condition_variable _cv;
};
}
3.semaphore.cpp
#include "semaphore.h"
namespace toolkit
{
/**
* @brief: constructor
*/
semaphore::semaphore(unsigned int count_lock /*= 0*/) : _cnt_lock(count_lock)
{
}
/**
* @brief: deconstructor
*/
semaphore::~semaphore()
{
}
/**
* @brief:
*/
void semaphore::wait()
{
std::unique_lock<decltype(_mtx)> lock(_mtx);
// to avoid spurious awakenings
while (!_cnt_lock)
{
_cv.wait(lock);
}
--_cnt_lock;
}
/**
* @brief:
*/
bool semaphore::try_wait()
{
std::unique_lock<decltype(_mtx)> lock(_mtx);
if (_cnt_lock)
{
--_cnt_lock;
return true;
}
return false;
}
/**
* @brief:
*/
void semaphore::notify()
{
std::unique_lock<decltype(_mtx)> lock(_mtx);
++_cnt_lock;
_cv.notify_one();
}
}
一个c++11自定义的信号量的更多相关文章
- iOS 11开发教程(三)运行第一个iOS 11程序
iOS 11开发教程(三)运行第一个iOS 11程序 运行iOS11程序 创建好项目之后,就可以运行这个项目中的程序了.单击运行按钮,如果程序没有任何问题的话,会看到如图1.6和1.7的运行效果. 图 ...
- iOS 11开发教程(二)编写第一个iOS 11应用
iOS 11开发教程(二)编写第一个iOS 11应用 编写第一个iOS 11应用 本节将以一个iOS 11应用程序为例,为开发者讲解如何使用Xcode 9.0去创建项目,以及iOS模拟器的一些功能.编 ...
- java:jsp: 一个简单的自定义标签 tld
java:jsp: 一个简单的自定义标签 tld 请注意,uri都是:http://www.tag.com/mytag,保持统一,要不然报错,不能访问 tld文件 <?xml version=& ...
- JAVA代码:生成一个集合,自定义大小,100以内的随机整数
JAVA代码:生成一个集合,自定义大小,100以内的随机整数 方法一:(Random类) package com.dawa.test; import java.util.ArrayList; impo ...
- Java多线程系列--“JUC锁”11之 Semaphore信号量的原理和示例
概要 本章,我们对JUC包中的信号量Semaphore进行学习.内容包括:Semaphore简介Semaphore数据结构Semaphore源码分析(基于JDK1.7.0_40)Semaphore示例 ...
- ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)
Styling the Action Bar If you want to implement a visual design that represents your app's brand, th ...
- 一个select元素自定义设计的新思路:appearance: none之后利用<>符号制造小箭头
最近工作时解决了一个前端小问题(如下图所示):在Safari中,select的控件之上有不和谐的灰色部分. 刚开始时我以为是backgrand或是border设置不当之类产生的问题,在搜索了很久之后终 ...
- Android Launcher分析和修改11——自定义分页指示器(paged_view_indicator)
Android4.0的Launcher自带了一个简单的分页指示器,就是Hotseat上面那个线段,这个本质上是一个ImageView利用.9.png图片做,效果实在是不太美观,用测试人员的话,太丑了. ...
- iOS开发多线程篇 11 —自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
随机推荐
- C 语言do while 循环
do while 循环小练习 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 int a ...
- Python—安装跟爬虫相关的包
舆情爬虫分析:硬件: 4台服务器,分别放redis.python爬虫.mysql和 kafka四大板块.软件:1. mysql2. redis #leap1 /usr/bin/redis- ...
- 日常Java 2021/10/21
Java Iterator(迭代器) 如果需要使用iterator类需要从java.util包中引入它 Java Iterator不是一个集合,它是一种访问集合的方法,用于迭代ArrayList和Ha ...
- 日常Java 2021/9/23
练习使用Math.random函数,以及JOptionPane.showMessageDialog(null,"字符串","Results",JOptionPa ...
- mysql explain using index condition
Using where:表示优化器需要通过索引回表查询数据:Using index:表示直接访问索引就足够获取到所需要的数据,不需要通过索引回表:Using index condition:在5.6版 ...
- 监测linux系统负载与CPU、内存、硬盘、用户数的shell脚本
本节主要内容: 利用Shell脚本来监控Linux系统的负载.CPU.内存.硬盘.用户登录数. 一.linux系统告警邮件脚本 # vim /scripts/sys-warning.sh #!/bin ...
- java中的原子操作类AtomicInteger及其实现原理
/** * 一,AtomicInteger 是如何实现原子操作的呢? * * 我们先来看一下getAndIncrement的源代码: * public final int getAndIncremen ...
- git 使用https方式进行 pull、push代码免密
由于网络原因我用ssh方法拉取代码每次都提示远程服务连接失败,因此我用了https方式去拉去代码. 这种方式拉取代码每次操作都要输入密码,为了解决这个问题做了一下操作: 在命令行输入 git conf ...
- 【Java 与数据库】How to Timeout JDBC Queries
How to Timeout JDBC Queries JDBC queries by default do not have any timeout, which means that a quer ...
- Controller返回类的自动识别,WEB-INF,jsp位置
Controller: @Controller@RequestMapping("/params")public class ParamsController { @RequestM ...