2021年8月3日

摘要: 互斥锁std::mutex是一种最常见的线程间同步的手段,但是在有些情况下不太高效。 假设想实现一个简单的消费者生产者模型,一个线程往队列中放入数据,一个线程往队列中取数据,取数据前需要判断一下队列中确实有数据,由于这个队列是线程间共享的,所以,需要使用互斥锁进行保护,一个线程在往队列添加数据的时候 阅读全文
posted @ 2021-08-03 18:05 tycoon3 阅读(187) 评论(0) 推荐(0)
摘要: /* *lock_guard C++源码 内容也比较简单 *私有化了拷贝构造和赋值拷贝 *在内部对锁和构造和析构进行了适配 */ template<class _Mutex> class lock_guard<_Mutex> { // specialization for a single mute 阅读全文
posted @ 2021-08-03 16:43 tycoon3 阅读(93) 评论(0) 推荐(0)
摘要: 将 emplace_back() 和 push_back() 中区别最大的程序拎出来看: _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, std::forward<_Args>(__args)...); // empl 阅读全文
posted @ 2021-08-03 14:50 tycoon3 阅读(260) 评论(0) 推荐(0)
摘要: #include <iostream> // std::cout #include <atomic> // std::atomic #include <thread> // std::thread #include <vector> // std::vector // a simple global 阅读全文
posted @ 2021-08-03 11:49 tycoon3 阅读(2910) 评论(0) 推荐(0)
摘要: #include <iostream> int getValue () { int ii = 10; return ii; } int main() { std::cout << getValue(); return 0; } root@ubuntu:~/c++# g++ -std=c++11 ri 阅读全文
posted @ 2021-08-03 10:49 tycoon3 阅读(72) 评论(0) 推荐(0)
摘要: #include <iostream> using namespace std; int main() { int n = 10; int &r1 = n; //auto推导 auto r2 = r1; r2 = 20; cout << n << ", " << r1 << ", " << r2 < 阅读全文
posted @ 2021-08-03 10:38 tycoon3 阅读(206) 评论(0) 推荐(0)

导航