摘要: 分支预测优化 !! 对宏的参数 x 做两次取非操作,这是为了将参数 x 转换为布尔类型 #if __GNUC__ >= 3 #define likely(x) (__builtin_expect(!!(x), 1)) #define unlikely(x) (__builtin_expect(!!( 阅读全文
posted @ 2021-01-21 18:33 Ray.floyd 阅读(87) 评论(0) 推荐(0) 编辑
摘要: 不使用 std::forward时,下述代码G不管传入什么类型的参数,只会最终调用 void F(int& a); using namespace std; void F(int& a) { cout << "int& version " <<a <<endl; } void F(int&& a) 阅读全文
posted @ 2021-01-21 17:34 Ray.floyd 阅读(3502) 评论(0) 推荐(1) 编辑
摘要: 先说 std::move的作用,std::move 就是帮助实现,当参数为左值的时候,如何调用 对象的移动构造函数而非拷贝构造函数。 移动构造函数与拷贝构造函数的区别: 拷贝构造的参数是const MyString& str,是常量左值引用,而移动构造的参数是MyString&& str,是右值引用 阅读全文
posted @ 2021-01-21 16:43 Ray.floyd 阅读(497) 评论(0) 推荐(0) 编辑
摘要: #include <functional> #include <iostream> void print_num(int i); inline void print_num(int i) { std::cout << i << '\n'; } struct PrintNum { void opera 阅读全文
posted @ 2021-01-21 16:28 Ray.floyd 阅读(379) 评论(0) 推荐(0) 编辑
摘要: boost 中有一个模板函数 type_id_with_cvr 可以将变量类型的描述信息打印出来。 std::is_same 用于判断两个类型是否一致,强规则判断模式,例如 const int != int; std::decay 用于判断两个类型是否一致,弱规则判断模式,例如 const int 阅读全文
posted @ 2021-01-21 13:53 Ray.floyd 阅读(467) 评论(0) 推荐(0) 编辑
摘要: 知识点 c++ std:call_once 可以用于保证某个函数在多线程环境下只被调用一次。 std::enable_if 泛型编程中模板最优匹配原则,template<bool Cond, class T = void> struct enable_if {}; //The type T is e 阅读全文
posted @ 2021-01-21 11:45 Ray.floyd 阅读(638) 评论(0) 推荐(0) 编辑
摘要: 知识点std::future 获取异步函数调用的结果。 std::result_of 获取函数模板的返回类型。 std::async 用于创建异步任务,实际上就是创建一个线程执行相应任std::launch::async, enable asynchronous evaluation 异步执行 st 阅读全文
posted @ 2021-01-21 10:39 Ray.floyd 阅读(502) 评论(0) 推荐(0) 编辑
摘要: std::floor //-->向下取整数 std::ceil // -->向上取整数: std::llround //最接近的整数 std::numeric_limits<double>::epsilon() //最小双精度小数 std::numeric_limits<int>::max() // 阅读全文
posted @ 2021-01-21 10:23 Ray.floyd 阅读(245) 评论(0) 推荐(0) 编辑
摘要: The main characteristic of atomic objects is that access to this contained value from different threads cannot cause data races。 提供对基本内建数据的互斥访问。 // at 阅读全文
posted @ 2021-01-21 10:17 Ray.floyd 阅读(393) 评论(0) 推荐(0) 编辑
摘要: A spinlock mutex can be implemented in userspace using an atomic_flag. 一旦标识对象初始化完成,只能做三种操作:销毁、清除或设置并查询其先前的值。 这些分别对应析构函数、clear()函数以及test_and_set()函数。 c 阅读全文
posted @ 2021-01-21 09:55 Ray.floyd 阅读(139) 评论(0) 推荐(0) 编辑
摘要: 1. Relaxed ordering: 在单个线程内,所有原子操作是顺序进行的。两个来自不同线程的原子操作是任意顺序的。 2. Release -- acquire: 来自不同线程的两个原子操作顺序限制,需要两个线程进行一下同步(synchronize-with)。同步对一个变量的读写操作。线程 阅读全文
posted @ 2021-01-21 09:41 Ray.floyd 阅读(187) 评论(0) 推荐(0) 编辑