muduo之Atomic
Atomic是muduo原子操作的实现类。
Atomic.h
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)#ifndef MUDUO_BASE_ATOMIC_H
#define MUDUO_BASE_ATOMIC_H#include "muduo/base/noncopyable.h"#include <stdint.h>namespace muduo
{namespace detail
{
template<typename T> //可以根据你所需要的类型就行匹配。其实就是模板
class AtomicIntegerT : noncopyable
{public:AtomicIntegerT(): value_(0){}// uncomment if you need copying and assignment//// AtomicIntegerT(const AtomicIntegerT& that)// : value_(that.get())// {}//// AtomicIntegerT& operator=(const AtomicIntegerT& that)// {// getAndSet(that.get());// return *this;// }T get(){// in gcc >= 4.7: __atomic_load_n(&value_, __ATOMIC_SEQ_CST)return __sync_val_compare_and_swap(&value_, 0, 0);}T getAndAdd(T x){// in gcc >= 4.7: __atomic_fetch_add(&value_, x, __ATOMIC_SEQ_CST)return __sync_fetch_and_add(&value_, x);}T addAndGet(T x){return getAndAdd(x) + x;}T incrementAndGet(){return addAndGet(1);}T decrementAndGet(){return addAndGet(-1);}void add(T x){getAndAdd(x);}void increment(){incrementAndGet();}void decrement(){decrementAndGet();}T getAndSet(T newValue){// in gcc >= 4.7: __atomic_exchange_n(&value, newValue, __ATOMIC_SEQ_CST)return __sync_lock_test_and_set(&value_, newValue);}private:volatile T value_;
};
} // namespace detailtypedef detail::AtomicIntegerT<int32_t> AtomicInt32;
typedef detail::AtomicIntegerT<int64_t> AtomicInt64;} // namespace muduo#endif // MUDUO_BASE_ATOMIC_H
muduo之Atomic相关推荐
- muduo之Thread
muduo也对线程进行了封装,下面看一下实现. Thread.h // Use of this source code is governed by a BSD-style license // th ...
- Muduo 网络编程示例之三:定时器
陈硕 (giantchen_AT_gmail) Blog.csdn.net/Solstice 这是<Muduo 网络编程示例>系列的第三篇文章. Muduo 全系列文章列表: http:/ ...
- muduo网络库之Acceptor、TcpServer
本篇博客针对Acceptor类和TcpServer类做下小结. 博客代码来自于陈硕的muduo网络库,github地址https://github.com/chenshuo/muduo 学习笔记: A ...
- 28muduo_net库源码分析(四)
1.定时器 (1)muduo的定时器由三个类实现,TimerId.Timer.TimerQueue,用户只能看到第一个类,其它两个都是内部实现细节 (2)TimerQueue的接口很简单,只有两个函数 ...
- 13muduo_base库源码分析(四)
1.线程标识符 (1)Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得 ...
- muduo之Connector
Connector负责主动发起连接,不负责创建socket,只负责连接的建立,外部调用Connector::start就可以发起连接,Connector具有重连的功能和停止连接的功能,连接成功建立后返 ...
- muduo之TcpClient
muduo用TcpClient发起连接,TcpClient有一个Connector连接器,TCPClient使用Conneccor发起连接, 连接建立成功后, 用socket创建TcpConnecti ...
- muduo之TimerQueue
muduo的TimerQueue是基于timerfd_create实现,这样超时很容易和epoll结合起来.等待超时事件保存在set集合中,注意set集合的有序性,从小到大排列,整个对TimerQue ...
- muduo之EventLoop
EventLoop.cc就相当于一个reactor,多线程之间的函数调用(用eventfd唤醒),epoll处理,超时队列处理,对channel的处理.运行loop的进程被称为IO线程,EventLo ...
最新文章
- R语言构建xgboost模型并评估模型(测试集、训练集每一轮):误分类率指标(misclassification rate)、logloss
- hexo的yilia主题鼠标点击出现小红心
- ARM 之五 发展史及各时期内核(ARM1 ~ ARM11 / Cortex)介绍
- [HIVE] shell调用hive客户端导致nohup后台进程stopped
- netflix 开源_Netflix的Polynote是一个新的开源框架,可用来构建更好的数据科学笔记本
- [转载MSDN]IIS 7.0中的Live Smooth Streaming -入门
- JavaScript学习总结(一)——JavaScript基础
- linux sudo永久免密码,linux 免密码 使用sudo 直接使用root权限执行命令
- 2021当一名优雅的代码打工人
- 运筹优化(十二)--带约束非线性规划(NLP)
- 19.简报配置(Newsletter Configuration)
- android 在图上绘制字符串,Android 使用Canvas在图片上绘制文字的方法
- 【图像隐写】基于matlab GUI DCT数字水印嵌入与提取【含Matlab源码 1323期】
- 咬了一口苹果死去的计算机之父——图灵
- 智能网联V2X全球发展现状以及V2X平台设计简介
- python常用颜色表示_Python命令行的常用颜色,python,字体
- 2020-12-11静态路由汇总实验
- 【C++】CGAL学习笔记
- java 批量读取excel表格内容_java 使用POI批量导入excel数据
- 生成EAN13条码(C语言实现)