- 博客(106)
- 资源 (18)
- 收藏
- 关注
原创 nodejs 设计思想杂记四 异步控制流模式
parallelvar tasks = [...];var completed = 0;tasks.forEach(function(task) {task(function() {if(++completed === tasks.length) {finish();}});async的使用 async.series(tasks, [callback])func
2016-07-07 17:15:28
938
原创 nodejs 设计思想杂记 三 observer模式
观察者模式:一个对象,当其状态改变时能够通知一系列的观察者。EventEmitter看图说话使用方法:var EventEmitter = require('events').EventEmitter;var eeInstance = new EventEmitter();提供有几个接口:on(event, listener): 对event与linst
2016-07-06 12:55:38
1698
原创 nodejs 单例模式实现
function Logger(name) {if(!(this instanceof Logger)) {return new Logger(name);}this.name = name;};
2016-07-06 00:12:43
3784
原创 nodejs设计思想杂技二 callback 模式
回调。。。。与return回调函数如何替代return的功能?同步的函数function add(a, b) {return a + b;}异步的函数function add(a, b, callback) {callback(a + b);}同步的函数执行后return异步的函数执行完将结果放入callback中同步
2016-07-05 23:03:25
721
原创 nodejs 设计思想杂记一 reactor模式
开始正题,reactor 模式nodejs关于异步的本质,以及隐藏在此模式背后的故事。如单进程单线程的架构、非阻塞IO。最后看下整个nodejs平台。IO很慢,相比于CPU的处理速度来说,在非密集型计算的应用场景下,IO是一个拖慢速度的瓶颈。当然,现在正火的深度学习可以一次跑上几个月。。。。这种计算密集型的应用场景下除外。一个传统的阻塞型io的例子,线程池中每个线程处理
2016-07-05 22:12:08
3160
原创 share_ptr的线程安全读写方式
class T { public: int get() { return t; } void set(int num) { t = num; } T():t(1) {}private: int t; };std::mutex g_mut;std::shared_ptr g_ptr;void func(const std::shared_ptr& pt) { std::coutge
2016-04-13 15:46:42
1816
原创 aop之应用 时间记录器及日志
struct TimeElapsedAspect{ void Before(int i) { m_lastTime = m_t.elapsed(); } void After(int i) { std::cout << "time elapsed: " << m_t.elapsed() - m_lastTime << "ms" <<std::endl; }private:
2015-12-11 11:19:35
801
原创 c++ 面向切面变成 aop 通用模板
#ifndef AOP_h#define AOP_h#define HAS_MEMBER(member)\templatestruct has_member_##member\{\private:\ template static auto Check(int) -> decltype(std::declval().member(std::declval()...), std::tr
2015-12-11 11:17:12
2066
原创 c++11 计时器
#pragma once#includeusing namespace std;using namespace std::chrono;class Timer{public: Timer() : m_begin(high_resolution_clock::now()) {} void reset() { m_begin = high_resolution_clock::now(
2015-12-11 11:12:28
2913
原创 对象池 c++11
#ifndef COBJECTPOOL_H#define COBJECTPOOL_H#include #include #include #include const int maxObjectNum = 10;templateclass ObjectPool { template using Consturctor = std::function(Args...)>;pub
2015-12-10 21:11:17
1256
原创 使用函数包装器的 通用泛化的命令类
//#ifndef COMMCOMMAND_H#define COMMCOMMAND_H#include#includetemplate class CommCommand{private: std::function m_f;public: //接受可调用对象的函数包装器 template::value>::type> void Wrap(F &&f, Args &
2015-12-10 16:57:31
1012
原创 std::forward 完美转发
templateinline auto FuncWrapper(Function &&f, Args &&...args){ return f(std::forward(args)...);}void hello(){ std::cout << "hello" << std::endl;}void hello1(std::string x){ std::cout << x
2015-12-10 16:23:01
753
原创 c++ final关键字
final关键字来限制类不能被继承,或者虚函数不能被重写。class A final {virtual void hehe() final;};如此,class A不能被其他类集成,hehe()函数也不能被重写
2015-12-08 14:19:06
2887
原创 c++ weak_ptr
weak_ptr是用来监视shared_ptr的,不会使引用计数增加。 std::shared_ptr t(new int(10)); std::weak_ptrwk(t); if(wk.expired()) std::cout<<"t is expired"<<std::endl; else std::cout
2015-12-08 13:40:26
530
原创 c++ tuple的基本操作
//create tuple std::tuple tp = std::make_tuple(2,2); int a,b; //get the value of tp std::tie(a,b) = tp; std::cout<<a+b<<std::endl; a = 0; //only get one value of tp std::tie(a,std::ignore) = t
2015-12-07 14:43:55
1999
原创 c++ lambda表达式捕获变量参数
[]不捕获任何变量[&]捕获外部作用域中所有变量,并作为引用在函数体重使用[=]捕获外部作用域中所有变量,并作为副本在函数体重使用[=,&foo]捕获外部作用域中所有变量,并作为副本在函数体重使用,对于foo按引用捕获[foo]当作副本捕获foo,不引入其他变量[this]捕获当前类中的this指针,让lambda表达式拥有和当前类成员函数同样的访问权限。如果已经使用了&或者=
2015-12-07 14:16:12
6593
原创 c++ std::bind 基本用法
#include void hello(int a){std::cout<<a<<std::endl;}void call_when(int x,const std::function &f){if(x == 0) f(x);} auto pt = std::bind(hello,std::placeholders::_1); call_when(0,pt);
2015-12-07 13:57:21
814
原创 c++ std::function作为参数传入函数
#include void hello(int a){std::cout<<a<<std::endl;}void call_when(int x,const std::function &f){if(x == 0) f(x);} call_when(0,hello);需要头文件functional,在std::function作为参数使用时,必须加con
2015-12-07 10:26:56
9287
原创 c++ 可调用对象
可调用对象分为如下几种:1 函数指针2 具有Operator()成员函数的类对象3 可被转换为函数指针的类对象4 类成员函数指针函数指针即函数的名字,直接调用即可具有operator成员函数的类对象,如下例所示:class foo{public: void operator()(void) {}};调用时:foo f;f();类的函数指
2015-12-07 10:16:56
604
原创 c++11,for,for each,std::for_each的应用
// cpp11exercise.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include void hello(int a){std::cout<<a<<std::endl;}int _tmain(int argc, _TCHAR* argv[]){ std::vector vv ; vv.
2015-12-04 19:36:22
12936
原创 在一个多线程系统中,主进程应该写什么?
在一个多线程的系统中,主线程应该不占资源,而且不应该结束。遵循此原则,在c++的系统中,用c++11的代码实现在主线程中等待主线程被唤醒,且不结束。#include #include std::mutex m_wt; std::unique_lock lk(m_wt); std::condition_variable wt;//在主线程结束之前 wt.wait(lk);
2015-12-02 10:52:13
1101
原创 c++ 并发系统访问 测试 调试方法/策略
如果在单核系统中没有错误,但是在多核系统或多处理器中出错,可能是竞争条件错误或者同步、内存顺序错误。测试实例的应用场景:1、在一个线程自身队列上调用push()或pop()来验证该队列工作基础级别2、在一个空队列上一个线程调用push(),另一个线程调用pop()3、在一个空队列上多个线程调用push()4、在一个满队列上多个线程调用push()5、在一个空队列上多个线程调
2015-12-02 10:32:58
618
原创 c++11,线程池之二--有等待线程池中任务完成功能的线程池
#include #include #include #include #include #include class function_wrapper{ struct impl_base { virtual void call()=0; virtual ~impl_base() {} }; std::unique_ptr i
2015-11-28 19:55:05
1171
原创 c++11 线程池系列之一 所需要的join_threads
class join_threads{std::vector &threads;public: explicit join_threads(std::vector &threads_):threads(threads_){} ~join_threads() { for(unsigned long i = 0 ; i < threads.size();++i) { if(threa
2015-11-28 16:52:19
2127
原创 c++11 线程池系列之一 所需要的thread_safe_queue
templateclass thread_safe_queue{private: mutable std::mutex mut; std::queue> data_queue; std::condition_variable data_con;public: thread_safe_queue(){} thread_safe_queue(thread_safe_queue con
2015-11-28 16:50:59
4233
原创 c++11 线程池系列之一 最简单的线程池
线程池最简单的形式是含有一个固定数量的工作线程来处理任务,典型的数量是std::thread::hardware_concurrency().当有任务要处理时,调用一个函数将任务放到等待队列中。每个工作线程都是从该队列中取出任务,执行完任务后继续从等待队列取出更多的任务来处理。在最简单的情况,没有办法来等待一个任务完成。如需要这样的功能,则需要用户自己维护同步。下面上代码class t
2015-11-28 16:48:59
2943
原创 c++11,std::find的并行化模板化
#include templateIterator parallel_find_inter(Iterator first,Iterator last,MatchType match,std::atomic& done){try{ unsigned long const length = std::distance(first,last); unsigned long const mi
2015-11-28 11:05:38
901
原创 c++11,for_each的并行化,改写模板
template void parallel_for_each(Iterator first,Iterator last,Func f){unsigned long const length = std::distance(first,last);if(!length) return;unsigned long const min_per_thread = 25;if(len
2015-11-28 10:48:35
1290
原创 udp打洞,c++实现,Nat
#pragma once#include // 定义iMessageType的值#define LOGIN 1#define LOGOUT 2#define P2PTRANS 3#define GETALLUSER 4// 服务器端口#define SERVER_PORT 6060// Client登录时向服务器发送的消息struct stLoginMessage{
2015-11-28 10:29:23
2528
1
原创 c++11baohan线程安全的队列
#include "stdafx.h"#include #include #include #include templateclass threadsafe_queue{private: mutable std::mutex mut; std::queue> data_queue; std::condition_variable data_con;public: th
2015-11-27 22:44:24
720
原创 c++11 packaged_task 用法,将任务打包
#include std::mutex m;std::deque> tasks;templatestd::future post_task(Fun f){std::packaged_task task(f);std::future ret = task.get_future();std::lock_guard lk(m);tasks.push_back(std::move(tas
2015-11-27 16:49:34
2267
1
原创 c++11 async 的自带参数使用
class X{public: int foo(int a,std::string const& b){std::cout<<a<<std::endl<<b<<std::endl;return 3;} std::string bar(std::string const& a){std::cout<<a<<std::endl;return a;}}; X x; //在新线程中运行
2015-11-26 21:21:57
1147
原创 c++11使用 async异步函数并传递参数以及auto的使用方法
class X{public: int foo(int a,std::string const& b){std::cout<<a<<std::endl<<b<<std::endl;return 3;} std::string bar(std::string const& a){std::cout<<a<<std::endl;return a;}}; X x; //传入x是传入x的副本
2015-11-26 21:07:51
5000
原创 c++11 async启动异步任务的使用方法
#include std::future theAnswer = std::async(ihello); std::cout<<theAnswer.get()<<std::endl;当调用get()时,线程会阻塞直到异步线程结束。
2015-11-26 20:21:09
1787
原创 c++11future简单使用及介绍
唯一future:std::future此实例是仅有的一个指向其关联事件的实例,类似std::unique_ptr.共享future:std::shared_future多个实例可指向一个事件 。两种future变为就绪状态后无法恢复future本身非线程安全的,需要用户本身实现访问时的线程安全。
2015-11-26 20:13:51
1743
原创 c++11线程安全的队列的类的定义
#include #include #include #include templateclass threadsafe_queue{private: mutable std::mutex mut; std::queue data_queue; std::condition_variable data_con;public: threadsafe_queue(){} t
2015-11-26 16:46:56
1990
原创 c++11condition_variable的wait与lock类型的匹配
wait与std::unique_lock搭配使用,std::lock_guard并不提供与wait搭配使用的灵活性。
2015-11-26 16:22:37
1694
原创 c++11conditon_variable的wait在类中的等待条件
class X{data_con.wait(std::mutext,[this]{return xx;};};
2015-11-26 16:19:01
1746
线程池,c++11,跨平台,支持vs12,g++最新的编译器,高效
2015-12-04
Transact-SQL语句基础
2014-12-26
信息检索导论答案 introduction to information retrieval
2014-09-16
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人