摘要:题目链接:https://leetcode-cn.com/problems/print-zero-even-odd/ 代码参考链接:https://leetcode-cn.com/problems/print-zero-even-odd/solution/c-san-chong-fang-shi-b
阅读全文
摘要://(1)补充一些知识点 //(1.1)虚假唤醒 // wait(), notify_one(), notify_all() 使用非常频繁的接口 // 虚假唤醒是指没有满足条件的时候被唤醒了。 //(1.2)atomic #include<iostream> #include<thread> #in
阅读全文
摘要://(1)windows临界区 #include<iostream> #include<thread> #include<vector> #include<list> #include<mutex> #include<windows.h> #define _WINDOWS //定义一个开关 usin
阅读全文
摘要://(1)原子操作std::atomic续谈 // 原子操作针对++,--,+=,-=,&=,|=,~=是支持的,其他的可能不支持。 #include<iostream> #include<mutex> #include<thread> #include<future> using namespac
阅读全文
摘要://(1)std::future的其他成员函数 #include<iostream> #include<mutex> #include<thread> #include<future> using namespace std; int my_thread() { cout << "my_thread
阅读全文
摘要://(1)std::async、 std::future创建后台任务并返回值 // async是一个函数模板,用来启动一个异步任务,返回一个future类型的对象。用future // 的get方法来获得线程的返回值。 // 异步任务:自动创建一个线程,并开始执行对应线程的入口函数。 #includ
阅读全文
摘要:/*condition_variable、wait、notify_one、notify_all*/ //(1)条件变量std::condition_variable()、wait()、notify_one()// std::condition_variable()是一个类,需要和互斥量配合工作。 #
阅读全文
摘要://(1)设计模式大概谈 // 使得程序灵活,但是代码阅读非常的晦涩。 //(2)单例设计模式 // 使用频率比较高。只能创建一个对象。 #include<iostream> using namespace std; class MyCAS { private: MyCAS() {}; //私有化构
阅读全文
摘要://(1)unique_lock取代lock_guard,unique_lock是一个类模板,比lock_guard更加灵活。// lock_guard取代了mutex的lock()和unlock()。unique_lock比lock_guard效率低一点,内存占用多一些。 #include<ios
阅读全文
摘要://(1)互斥量的(mutex)的基本概念 // 就是一个类的对象。多个线程可以尝试对一个mutex的对象进行lock,但是只有一个线程成功,其他线程需要等待。 //(2)互斥量的用法 //(2.1)lock(), unlock():要成对使用 #include<iostream> #include
阅读全文
摘要://(1)创建和等待多个线程 #include<iostream> #include<thread> #include<vector> using namespace std; void myprint(int num) { cout << "this thread id = " << std::t
阅读全文
摘要:/*线程传参详解,detach()大坑,成员函数做线程函数*/ //(1)传递临时对象作为线程参数 //(1.1)要避免的陷阱 #include<iostream> #include<thread> using namespace std; void myprint(const int& i, co
阅读全文
摘要:/*线程启动、结束,创建线程多法,join,detach*/ //(1)线程运行的开始和结束 //主线程从main为入口。那我们自己创建的线程也需要从一个函数为入口。 // 当没有detach的时候,整个程序执行结束的标志是主线程执行结束。 //(1.1)thread #include<iostre
阅读全文
摘要:/*第一节 并发基本概念与实现,进程、线程基本概念*/ //(1)并发、进程、线程的基本概念和综述 //(1.1)并发:两个或者更多的任务同时进行,一个程序同时执行多个独立的任务。 // 单核cpu某一个时刻只能执行一个任务;单核cpu实行多任务的方式是进行任务切换实现的。 // 这是一种并发的假象
阅读全文