![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++
今天也是元气满满的一天
害怕是因为无知
展开
-
C++ Future and Promise
C++ future and promise and async():本文详细介绍C++的future, promise以及async()功能。使用这几个功能需要在source code里面加上#include<future>一个简单的例子:新建一个线程,计算N的阶乘,并存储到x中。一个简单的实现是这个样子的:void factorial(int N, int& x){ int res = 1; for(int i = N; i > 1; i--){ res翻译 2020-11-30 05:10:34 · 378 阅读 · 0 评论 -
C++ Multithreading(4) -- event handling
Event handlingEvent handling in C++ multithreading:global variablecondition variableEvent handling in C++ multithreading:当有多个线程同时存在的时候,可能一个线程需要等另一个线程完成某个任务,或者等某一个条件被设置为真才可以继续运行。比如:线程1负责处理数据线程2负责读取数据那么线程1一定要等到线程2读取到了所需数据之后,才可以继续执行。假如我们有一个应用,这个应用有三个ta原创 2020-06-12 10:11:09 · 309 阅读 · 0 评论 -
C++ Multithreading(3) -- passing arguments to threads
线程传参passing arguments to threadscaveat:pass stack-located local variable to threadspass heap-located variable to threadshow to pass by reference:pass pointer to member function of a class as thread functionpassing arguments to threads给thread传参很简单,直接将call原创 2020-06-12 01:19:45 · 311 阅读 · 0 评论 -
C++ Multithreading (2) -- joining/detaching threads
本文介绍如下几点create and join threadscreatejoincreate a thread using function pointercreate a thread using function objectcreate a thread using Lambda functionget thread IDdetach threadspass arguments to th...原创 2020-06-05 11:14:00 · 205 阅读 · 0 评论 -
C++ alias template
C++ alias templatetype aliastypedefusingtypedef和using的区别alias template:type aliastype alias就是将某一个type重新命名为另一个更加直观简单的名字。C++中有两种type alias:typedeftypedef [original-type] [your-alias];比如:typedef int Pixel;Pixel p = 1; // 等价于 int p = 1;typedef std::m原创 2020-06-05 10:02:10 · 742 阅读 · 0 评论 -
C++ Multithreading (1) -- creating threads
Three ways to create threadsintroductionthread creationcreating threads with function pointercreating threads with function objectscreating threads with lambda functionintroduction一开始,C++是不支持多线程的,直到C++ 11开始,才真正引入了多线程这个概念和标准库。如果想在cpp文件,比如project.cpp里面使用多原创 2020-06-05 09:25:00 · 304 阅读 · 0 评论 -
C++ getopt
本文讲解如何处理命令行传入的选项和参数getopt()getopt_long()getopt_long_only()example比如我们执行如下指令会输出file文件的前10行:head file // 默认输出file的前10行如果我们想要输出文件的前50行,就可以加选项"-n"和参数50head -n 50 file通过传入-n 50, head程序就知道我们想要得到是前50行...翻译 2020-04-25 08:58:36 · 1271 阅读 · 0 评论 -
C++ Lambda function
Lambda FuctionDefinitionNeed of Lambda functionsHow to pass outer scope elements inside Lambda functionsDefinitionlambda function也可以称之为匿名function,你可以向lambda function传入参数,lambda function也可以返回参数,它和fun...原创 2020-04-23 08:51:05 · 2073 阅读 · 0 评论 -
C++ Function object
本文介绍C++ standard library中的function objectdefinitioncreate a function objectfunction object VS function优势1:优势2:definition只要一个class/struct实现了operator(),就可以称之为function object(或者functor)。create a funct...原创 2020-04-23 08:17:32 · 661 阅读 · 0 评论 -
C++ Smart pointer
Smart pointer in C++什么是smart pointer为什么要使用smart pointer常用的smart pointer以及使用方法shared pointersunique pointerssmart pointer背后的故事什么是smart pointer一般在C/C++中,如果我们使用了pointer来指向某块heap区域,当不再需要这块区域的时候,我们需要手动删除...原创 2020-04-16 03:40:04 · 462 阅读 · 0 评论 -
C++ Condition variable
如果你的线程需要改变某个变量,改动完成之后通知另一个线程持续等待,直到收到某一个线程发来的通知那么就可以放心食用std::condition_variable了。下面用一个例子来介绍什么是condition variable以及怎么使用:妈妈和儿子的故事:儿子在外面玩,衣服脏了。儿子说:“妈妈帮我洗衣服”。妈妈说:“好的吧”。那么总结起来,可以用两个functions来表示这...翻译 2020-04-13 23:36:22 · 402 阅读 · 0 评论 -
C++ Locks
C++ Locksstd::mutexdeadlockstd::lock_guardTo avoid deadlockstd::unique_lockTo avoid deadlockTo get higher parallelizationConditional variable requires std::unique_lockstd::mutexmutex是C++中的一个可以加锁的obj...原创 2020-04-13 12:01:21 · 353 阅读 · 0 评论 -
C++ keywork explicit
最近code的时候遇到一个warning,虽然不是error,但是也值得好好说一下:Warning: Single-argument constructors must be marked explicit to avoid unintentional implicit conversions在之前的文章中谈到了implicit conversion和operator conversion...原创 2020-04-12 07:22:04 · 390 阅读 · 0 评论 -
C++ conversion operator
本文主要会介绍:implicit conversionconversion operatorimplicit conversion有时候我们传入的参数并不是function想要的类型,但是依然可以运行,这是因为C++ compiler在底层悄悄帮我们进行了type conversion,比如:class entity{private: int a;public: en...原创 2020-04-12 05:48:11 · 1217 阅读 · 1 评论 -
C++ object initialization
在C++中,即使是非常简单的初始化也有很多需要了解的东西,总结起来,一共分为以下几个部分:initializer listuniform initializationcopy initializationInitializer list在C++ 11之前,可以用一组数值来初始化一个C类型的数组,但是如果想要初始化一个vector, map或者任何其他类型的container, 只...原创 2020-04-12 03:35:11 · 411 阅读 · 0 评论