C++并发
luixiao1220
这个作者很懒,什么都没留下…
展开
-
C++并发编程(CH05)[内存模型初步&atomic-01&02]
Memory model basicsC++内存模型,包含有两个方面对象的内存布局多线程方面Objects and memory locationsC++对象的定义是(内存中的一个区域)struct my_data{ int i; double d; unsigned bf1:10; int bf2:25; int bf3:0; i...原创 2020-03-25 12:18:32 · 435 阅读 · 0 评论 -
C++并发编程(CH04)[利用同步来简化代码-04]
author:luixiao1223title: chapter04Using synchronization of operations to simplify codeFunctional programming with futures函数式编程的概念是,函数不依赖与外部变量,只和参数有关系。只要参数相同那么函数的结果就像。如果使用future,各个thread之间靠fut...原创 2020-03-17 10:13:12 · 625 阅读 · 1 评论 -
C++并发编程(CH04)[clock-03]
Waiting with a time limit两种等待时间,一种时等待多长时间然后返回,一种是等待到某这个时刻返回,std::condition_variable有对他们各有两种实现支持.Clocksclock是一个class.它提供以下四种信息.The time nowstd::chrono::system_clock::now()The type of the val...原创 2020-03-12 10:18:54 · 205 阅读 · 0 评论 -
C++并发编程(CH04)[future-02]
Waiting for one-off events with futuresstd::shared_future和std::future 的行为和std::shared_ptr和std::unique_ptr类似.future一旦变成ready状态.就不能reset状态了.这两个future都是模板.模板参数就是关联数据.std::future<void> 表示没有关联数...原创 2020-03-12 09:30:18 · 216 阅读 · 0 评论 -
C++并发编程(CH04)[条件变量wait-01]
Waiting for an event or other condition如何去查询数据是否已经准备好了?用一个死循环不断的去测试完成flag.间隔一段时间去测试完成flag.bool flag;std::mutex m;void wait_for_flag(){ std::unique_lock<std::mutex> lk(m); while(!fl...原创 2020-03-12 09:25:55 · 1112 阅读 · 0 评论 -
C++并发编程(附录A)[Lambda Functions-05]
author:luixiao1223title: 附录AA.5 Lambda Functions举例[]{ do_stuff(); do_more_stuff();}(); // call参数std::vector<int> data=make_data();std::for_each(data.begin(),data.end(),[](i...原创 2020-03-11 06:43:32 · 120 阅读 · 0 评论 -
C++并发编程(附录A)[ constexpr-04]
author:luixiao1223title: 附录AA.4 constexpr functionsconstconst int i=23;const int two_i=i*2;const int four=4;const int forty_two=two_i-four;Specify the bounds of an array:int bounds=99...原创 2020-03-10 17:50:06 · 178 阅读 · 0 评论 -
C++并发编程(CH03)[其它保护共享数据的手段-03]
Alternative facilities for protecting shared dataProtecting shared data during initializationstd::shared_ptr<some_resource> resource_ptr;void foo(){ if(!resource_ptr) { resource_...原创 2020-03-09 10:10:51 · 179 阅读 · 0 评论 -
C++并发编程(CH03)[用mutex来保护共享数据-01&02]
Problems with sharing data between threads如果多个线程只是并发的读数据.这不会导致竞争.Race conditions有的写竞争也不会导致严重的问题.比如往同一个队列中push对象的时候.谁先谁后其实不重要.但是为了修改链表的竞争就非常严重了.会导致不明确行为.Avoiding problematic race conditions...原创 2020-03-09 08:40:36 · 312 阅读 · 0 评论 -
C++并发编程(附录A)[ 默认函数关键字-03]
author:luixiao1223title: 附录AA.3 Default functions需要default的原因default关键字用于标定编译器自动生成的函数.你之所以要标定这些函数的原因有以下几点.改变默认函数的属性.因为默认函数是public.如果你想改成private或者protected.那么使用default关键字可以更好的文档标注性.如果你想告...原创 2020-03-05 13:25:15 · 134 阅读 · 0 评论 -
C++并发编程(附录A)[ delete函数-02]
如何防止一个类对象的拷贝和赋值函数被外部调用?老做法class no_copies{public: no_copies(){}private: no_copies(no_copies const&); no_copies& operator=(no_copies const&);};no_copies a;no_copies b(...原创 2020-03-04 14:35:29 · 108 阅读 · 0 评论 -
C++并发编程(CH02)[ 动态确定线程数量&线程id-03&04]
author:luixiao1223title: chapter02Choosing the number of threads at runtimethread::hardware_concurrency()可以获得硬件支持的线程数量,不过也可能返回0表示,系统为提供此类接口)但是这个只是参考值.实际不一定能够准确.为什么需要知道cpu支持的物理线程数量?因为过多的线程运行在...原创 2020-03-04 13:59:32 · 567 阅读 · 0 评论 -
C++并发编程(附录A)[ 右值引用-01]
author:luixiao1223title: 附录AA.1 Rvalue referencesint var=42;int& ref=var; // 左值引用ref=99;assert(var==99);左值和右值.从简单来说左值就是在内存中有确定地址,长期贮存的元素.而右值普遍是临时对象.虽然在内存中存在,但是只是临时的.比如常亮const char* ...原创 2020-03-03 13:12:43 · 96 阅读 · 0 评论 -
C++并发编程(CH02)[ 转移thread所有权-03]
move 示例std::ifstream是movable but not copyable,同理thread也是这样的对象.void some_function();void some_other_function();std::thread t1(some_function);std::thread t2=std::move(t1);t1=std::thread(some_other...原创 2020-03-03 10:22:00 · 196 阅读 · 0 评论 -
C++并发编程(CH02)[ 传递参数-02]
拷贝在默认情况下,参数是被拷贝进入thread的内部存储区的.且是按照右值传递给callable对象的.void f(int i,std::string const& s);std::thread t(f,3,"hello");//(sai:此处"hello",是被拷贝过去的。不是引用,因为这个机制设计时要小心)问题代码void f(int i,std::string con...原创 2020-03-02 15:20:50 · 158 阅读 · 0 评论 -
C++并发编程(CH02)启动thread-01]
Launching a thread所有callable对象都可以作为thread的参数void do_some_work();std::thread my_thread(do_some_work);class background_task{public: void operator()() const { do_something(); do_some...原创 2020-03-01 13:01:02 · 291 阅读 · 0 评论 -
C++并发编程(CH01)
author:luixiao1223title: chapter01简介Erlang语言编程就是使用进程进行来实现并发。使用进程来实现并发的方式具有部署在不同物理机器上得优点,底层使用网络进行通信。C++11允许可以不依赖于平台扩展来写多线程程序分治法(两种)一种是功能逻辑分开执行一种是处理不同的数据区块每个线程的栈开销大概是1MB引入多线程设...原创 2020-02-29 20:24:55 · 512 阅读 · 0 评论