c++
懵圈丰
这个作者很懒,什么都没留下…
展开
-
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 · 1750 阅读 · 0 评论 -
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 · 2044 阅读 · 0 评论 -
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 · 1224 阅读 · 0 评论 -
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 · 2408 阅读 · 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 · 674 阅读 · 0 评论 -
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 · 2205 阅读 · 1 评论 -
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 · 4104 阅读 · 0 评论 -
c++11 线程池系列之一 最简单的线程池
线程池最简单的形式是含有一个固定数量的工作线程来处理任务,典型的数量是std::thread::hardware_concurrency().当有任务要处理时,调用一个函数将任务放到等待队列中。每个工作线程都是从该队列中取出任务,执行完任务后继续从等待队列取出更多的任务来处理。在最简单的情况,没有办法来等待一个任务完成。如需要这样的功能,则需要用户自己维护同步。下面上代码class t原创 2015-11-28 16:48:59 · 2883 阅读 · 0 评论 -
c++11只调用一次的函数
std::once_flag flag;std::call_once(flag,func_address);原创 2015-11-26 11:23:21 · 2362 阅读 · 0 评论 -
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 · 1085 阅读 · 0 评论 -
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 · 1097 阅读 · 0 评论 -
c++ 并发系统访问 测试 调试方法/策略
如果在单核系统中没有错误,但是在多核系统或多处理器中出错,可能是竞争条件错误或者同步、内存顺序错误。测试实例的应用场景:1、在一个线程自身队列上调用push()或pop()来验证该队列工作基础级别2、在一个空队列上一个线程调用push(),另一个线程调用pop()3、在一个空队列上多个线程调用push()4、在一个满队列上多个线程调用push()5、在一个空队列上多个线程调原创 2015-12-02 10:32:58 · 581 阅读 · 0 评论 -
c++ final关键字
final关键字来限制类不能被继承,或者虚函数不能被重写。class A final {virtual void hehe() final;};如此,class A不能被其他类集成,hehe()函数也不能被重写原创 2015-12-08 14:19:06 · 2823 阅读 · 0 评论 -
c++ lambda表达式捕获变量参数
[]不捕获任何变量[&]捕获外部作用域中所有变量,并作为引用在函数体重使用[=]捕获外部作用域中所有变量,并作为副本在函数体重使用[=,&foo]捕获外部作用域中所有变量,并作为副本在函数体重使用,对于foo按引用捕获[foo]当作副本捕获foo,不引入其他变量[this]捕获当前类中的this指针,让lambda表达式拥有和当前类成员函数同样的访问权限。如果已经使用了&或者=原创 2015-12-07 14:16:12 · 6507 阅读 · 0 评论 -
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 · 743 阅读 · 0 评论 -
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 · 474 阅读 · 0 评论 -
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 · 1892 阅读 · 0 评论 -
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 · 9150 阅读 · 0 评论 -
c++ 可调用对象
可调用对象分为如下几种:1 函数指针2 具有Operator()成员函数的类对象3 可被转换为函数指针的类对象4 类成员函数指针函数指针即函数的名字,直接调用即可具有operator成员函数的类对象,如下例所示:class foo{public: void operator()(void) {}};调用时:foo f;f();类的函数指原创 2015-12-07 10:16:56 · 552 阅读 · 0 评论 -
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 · 12870 阅读 · 0 评论 -
在一个多线程系统中,主进程应该写什么?
在一个多线程的系统中,主线程应该不占资源,而且不应该结束。遵循此原则,在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 · 1058 阅读 · 0 评论 -
c++共享锁的使用
c++11未提供共享锁的实现,先使用boost的共享锁。#include boost::shared_mutex _mutex;boost::shared_lock mylock(_mutex);原创 2015-11-26 11:31:26 · 1073 阅读 · 0 评论 -
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 · 4838 阅读 · 0 评论 -
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 · 2833 阅读 · 0 评论 -
使用函数包装器的 通用泛化的命令类
//#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 · 951 阅读 · 0 评论 -
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 · 2010 阅读 · 0 评论 -
对象池 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 · 1159 阅读 · 0 评论 -
第一个 c++11多线程
// Consolehello.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include void hello(){std::cout}int _tmain(int argc, _TCHAR* argv[]){for(int k = 0 ; k {std::原创 2015-11-18 22:47:58 · 501 阅读 · 0 评论 -
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 · 756 阅读 · 0 评论 -
线程参数之 unique_ptr
void process(std::unique_ptr);std::unique_ptr p (new obj);std::thread t(process,std::move(p));p的所有权先进入 thread t,而后进入process.原创 2015-11-24 09:58:44 · 1296 阅读 · 0 评论 -
c++调试重要武器GetLastError
GetLastError();通过获取错误码,快速定位错误原创 2015-11-24 11:01:19 · 712 阅读 · 0 评论 -
线程的引用
线程 std::thread无视了引用参数,将自动改为副本func(para& pa);如要传入引用参数应使用std::thread t(func,std::ref(parameter));原创 2015-11-24 09:52:38 · 564 阅读 · 0 评论 -
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 · 675 阅读 · 0 评论 -
c++函数模板于类中的应用
class templateTest{public: template void process(func function) { function(); }}; templateTest tt; tt.process(hello);void hello(){ std::cout<<"hello"<<std::endl;}原创 2015-11-26 10:32:11 · 438 阅读 · 0 评论 -
c++11 async启动异步任务的使用方法
#include std::future theAnswer = std::async(ihello); std::cout<<theAnswer.get()<<std::endl;当调用get()时,线程会阻塞直到异步线程结束。原创 2015-11-26 20:21:09 · 1732 阅读 · 0 评论 -
c++11conditon_variable的wait在类中的等待条件
class X{data_con.wait(std::mutext,[this]{return xx;};};原创 2015-11-26 16:19:01 · 1710 阅读 · 0 评论 -
c++11细粒度的线程安全
void smallLockSize(){ std::unique_lock mylock(_mutex); hello(); mylock.unlock(); hello(); mylock.lock(); hello();}只有unique_lock可以使用细粒度的划分,即使用unlock以及locklock_guard等不可以原创 2015-11-26 11:03:27 · 541 阅读 · 0 评论 -
c++11线程管理,RAII方式等待异常环境下线程结束
直接上代码class thread_guard{private: std::thread& t;public: explicit thread_guard(std::thread& _t):t(_t){} ~thread_guard() { if(t.joinable()) t.join(); }private: thread_guard(thread_guard原创 2015-11-21 16:33:10 · 1387 阅读 · 0 评论 -
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 · 1925 阅读 · 0 评论 -
c++11condition_variable的wait与lock类型的匹配
wait与std::unique_lock搭配使用,std::lock_guard并不提供与wait搭配使用的灵活性。原创 2015-11-26 16:22:37 · 1647 阅读 · 0 评论