- 博客(29)
- 资源 (18)
- 收藏
- 关注
原创 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 1075
原创 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 2018
原创 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 4057
原创 c++11 线程池系列之一 最简单的线程池
线程池最简单的形式是含有一个固定数量的工作线程来处理任务,典型的数量是std::thread::hardware_concurrency().当有任务要处理时,调用一个函数将任务放到等待队列中。每个工作线程都是从该队列中取出任务,执行完任务后继续从等待队列取出更多的任务来处理。在最简单的情况,没有办法来等待一个任务完成。如需要这样的功能,则需要用户自己维护同步。下面上代码class t
2015-11-28 16:48:59 2870
原创 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 837
原创 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 1213
原创 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 2354 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 661
原创 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 2195 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 1074
原创 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 4750
原创 c++11 async启动异步任务的使用方法
#include std::future theAnswer = std::async(ihello); std::cout<<theAnswer.get()<<std::endl;当调用get()时,线程会阻塞直到异步线程结束。
2015-11-26 20:21:09 1716
原创 c++11future简单使用及介绍
唯一future:std::future此实例是仅有的一个指向其关联事件的实例,类似std::unique_ptr.共享future:std::shared_future多个实例可指向一个事件 。两种future变为就绪状态后无法恢复future本身非线程安全的,需要用户本身实现访问时的线程安全。
2015-11-26 20:13:51 1675
原创 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 1918
原创 c++11condition_variable的wait与lock类型的匹配
wait与std::unique_lock搭配使用,std::lock_guard并不提供与wait搭配使用的灵活性。
2015-11-26 16:22:37 1639
原创 c++11conditon_variable的wait在类中的等待条件
class X{data_con.wait(std::mutext,[this]{return xx;};};
2015-11-26 16:19:01 1699
原创 c++11条件变量的使用,condition_variable
void thread_prepare(int T){std::lock_guard lk(mt);data_queue.push(T);data_con.notify_one();}void thread_process(){while(1){std::unique_lock lk(mt);data_con.wait(lk,[]{return !data_queue.
2015-11-26 15:55:24 2168
原创 c++共享锁的使用
c++11未提供共享锁的实现,先使用boost的共享锁。#include boost::shared_mutex _mutex;boost::shared_lock mylock(_mutex);
2015-11-26 11:31:26 1059
原创 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 534
原创 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 435
原创 c++11的mutex
#include #include std::list _list;std::mutex _mutex;void addList(int iValue){std::lock_guard guard(_mutex);_list.push_back(iValue);}bool list_contain(int valueToFind){std::lock_guard guard
2015-11-26 10:24:33 409
原创 vector 中生成一大批线程并等待完成
void f(){ std::vector threads; for(int j = 0 ; j <= 20 ; j++) //生成匿名线程添加到vector中 threads.push_back(std::thread(hello)); //调用每个thread的join std::for_each(threads.begin(),threads.end(),std::mem_
2015-11-24 10:58:56 1062
原创 线程参数之 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 1282
原创 线程的引用
线程 std::thread无视了引用参数,将自动改为副本func(para& pa);如要传入引用参数应使用std::thread t(func,std::ref(parameter));
2015-11-24 09:52:38 559
原创 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 1378
原创 第一个 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 495
线程池,c++11,跨平台,支持vs12,g++最新的编译器,高效
2015-12-04
Transact-SQL语句基础
2014-12-26
信息检索导论答案 introduction to information retrieval
2014-09-16
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人