原创 在一個多線程系統中,主進程應該寫什麼?

在一個多線程的系統中,主線程應該不佔資源,而且不應該結束。 遵循此原則,在c++的系統中,用c++11的代碼實現在主線程中等待主線程被喚醒,且不結束。 #include <condition_variable> #include <mut

原创 c++11conditon_variable的wait在類中的等待條件

class X { data_con.wait(std::mutext,[this]{return xx;}; };

原创 c++ 併發系統訪問 測試 調試方法/策略

如果在單核系統中沒有錯誤,但是在多核系統或多處理器中出錯,可能是競爭條件錯誤或者同步、內存順序錯誤。 測試實例的應用場景: 1、在一個線程自身隊列上調用push()或pop()來驗證該隊列工作基礎級別 2、在一個空隊列上一個線程調用pus

原创 c++11baohan線程安全的隊列

#include "stdafx.h" #include <queue> #include <mutex> #include <condition_variable> #include <memory> template<class T

原创 c++11condition_variable的wait與lock類型的匹配

wait與std::unique_lock搭配使用, std::lock_guard並不提供與wait搭配使用的靈活性。

原创 udp打洞,c++實現,Nat

#pragma once #include <list> // 定義iMessageType的值 #define LOGIN 1 #define LOGOUT 2 #define P2PTRANS 3 #define GETALLUS

原创 c++11,線程池之二--有等待線程池中任務完成功能的線程池

#include <deque> #include <future> #include <memory> #include <functional> #include <iostream> #include <iostream> cla

原创 c++11線程安全的隊列的類的定義

#include <queue> #include <mutex> #include <condition_variable> #include <memory> template<class T> class threadsafe_q

原创 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 ba

原创 c++11,std::find的並行化模板化

#include <atomic> template<typename Iterator,typename MatchType> Iterator parallel_find_inter(Iterator first,Iterator l

原创 c++11 線程池系列之一 所需要的join_threads

class join_threads { std::vector<std::thread> &threads; public: explicit join_threads(std::vector<std::thread> &thread

原创 c++11 線程池系列之一 所需要的thread_safe_queue

template<class T> class thread_safe_queue { private: mutable std::mutex mut; std::queue<std::shared_ptr<T>> data_queu

原创 c++11 線程池系列之一 最簡單的線程池

線程池最簡單的形式是含有一個固定數量的工作線程來處理任務,典型的數量是std::thread::hardware_concurrency(). 當有任務要處理時,調用一個函數將任務放到等待隊列中。每個工作線程都是從該隊列中取出任務,執行完

原创 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 ba