C++11中async中future用法(一)
摘要:async意味着异步执行代码,看如下示例: #include #include #include #include #include #include using namespace std; int do_something(char c) { std::default_random_engine dre(c); std::uniform_int_dist...
阅读全文
posted @
2015-12-31 21:11
inevermore
阅读(2436)
推荐(0)
探究加法操作的原子性
摘要:加法在多线程下是否可靠? 我们先看下面的实例: #include #include #include #include #include #include using namespace std; int g_count = 0; int main(int argc, const char *argv[])
{ vector> ths; for ...
阅读全文
posted @
2015-02-09 21:41
inevermore
阅读(1105)
推荐(0)
C++11之function模板和bind函数适配器
摘要:在C++98中,可以使用函数指针,调用函数,可以参考之前的一篇文章:类的成员函数指针和mem_fun适配器的用法。 简单的函数调用 对于函数: void foo(const string &s)
{ cout f = &foo; f("bar"); 再看另外一个例子: void foo(int i, double d)
{ cout f =...
阅读全文
posted @
2014-10-17 22:12
inevermore
阅读(856)
推荐(1)
C++11之右值引用(三):使用C++11编写string类以及“异常安全”的=运算符
摘要:前面两节,说明了右值引用和它的作用。下面通过一个string类的编写,来说明右值引用的使用。 相对于C++98,主要是多了移动构造函数和移动赋值运算符。 先给出一个简要的声明: class String
{
public: String(); String(const char *s); //转化语义 String(const String &s); S...
阅读全文
posted @
2014-10-17 20:36
inevermore
阅读(866)
推荐(0)
C++11之右值引用(二):右值引用与移动语义
摘要:上节我们提出了右值引用,可以用来区分右值,那么这有什么用处? 问题来源 我们先看一个C++中被人诟病已久的问题: 我把某文件的内容读取到vector中,用函数如何封装? 大部分人的做法是: void readFile(const string &filename, vector &words)
{ words.clear(); //read XXXXX
...
阅读全文
posted @
2014-10-16 22:51
inevermore
阅读(514)
推荐(0)