随笔分类 -  C++Standard Library

C++标准库,包含(C++11, 14, 17, 20)
摘要:C++11标准库的条件变量为我们实现多线程直接通信带来的变量,如果对其提供的函数使用不当会给程序带来隐藏的问题。比如:伪唤醒和唤醒丢失问题。 一、什么是伪唤醒和唤醒丢失 先看代码如何使用条件变量: 1 std::condition_variable cv; 2 std::mutex gMtx; 3 阅读全文
posted @ 2021-07-03 21:51 blackstar666 阅读(5371) 评论(0) 推荐(0)
摘要:在现代C++中,我们一般使用std::bind获取lambda表达式构造一个函数对象,然后直接调用或者作为形参供其他函数调用。那同学们是否有使用过std::mem_fn这个模板函数,我们该如何正确使用它? 一、std::mem_fn作用 std::mem_fn官方文档介绍是这样的:std::mem_ 阅读全文
posted @ 2021-05-22 15:51 blackstar666 阅读(2881) 评论(0) 推荐(0)
摘要:玩过C++shared_ptr类型的智能指针的同学,是否有接触过std::enable_shared_from_this,它的出现为我们提供哪些编程方面的便利呢?下面就介绍它。 一、std::enable_shared_from_this的作用 按照enable_shared_from_this - 阅读全文
posted @ 2021-05-13 23:22 blackstar666 阅读(2834) 评论(0) 推荐(0)
摘要:一、Lambda表达式定义 二、Lambda捕获方式 三、Lambda使用 1 void main() 2 { 3 int a = 1, b = 2; 4 auto fn1 = []() {std::cout << "fn1\n"; }; 5 // auto fn1 = []() {std::cou 阅读全文
posted @ 2021-04-21 18:18 blackstar666 阅读(115) 评论(0) 推荐(0)
摘要:一、lock_guard和unique_lock锁 在并发编程中,我们常常使用标准库中的mutex互斥对象实现线程同步;但是如果在开发中裸用mutex对象,当某个线程锁住临界资源或临界区且在未解锁的情况下异常退出,那么其他线程将始终无法访问临界资源或临界区。lock_guard和unique_loc 阅读全文
posted @ 2021-04-06 22:39 blackstar666 阅读(587) 评论(0) 推荐(0)
摘要:一、背景 在C++标准库(第2版)第10章中的10.1.4 Predicate(判断式) VS.Function Object(函数对象)有这么个例子(gcc 4.5 visual C++2010): 1 class Nth { 2 public: 3 Nth(int nth) : _nth(nth 阅读全文
posted @ 2021-03-14 11:53 blackstar666 阅读(351) 评论(0) 推荐(0)
摘要:一、问题引入 有如下一段代码: 1 auto MakeGuard(HINTERNET hinternet) 2 { 3 if(!hinternet) { 4 const DWORD ec = ::GetLastError(); 5 throw std::system_error(ec, std::s 阅读全文
posted @ 2021-03-10 23:35 blackstar666 阅读(1576) 评论(0) 推荐(0)