boost::asiohtml
可用於如socket等IO對象的同步或異步操做,ios
應用程序必須有一個io_service對象. io_service對象負責鏈接應用程序與操做系統的IO服務.app
boost::asio::io_service io_service;異步
要執行IO操做應用程序須要一個像TCP Socket的IO對象:socket
boost::asio::ip::tcp::socket socket(io_service);tcp
然後執行同步鏈接操做,發送以下事件:函數
1. 應用程序調用IO對象的初始化鏈接操做:lua
socket.connect(server_endpoint);spa
2. IO對象向io_service 提出請求.操作系統
3. io_service 調用操做系統的功能執行鏈接操做.
4. 操做系統向io_service 返回執行結果.
5. io_service將錯誤的操做結果翻譯爲boost::system::error_code類型. error_code可與特定值進行比較,或做爲boolean值檢測(false表示無錯誤).結果再傳遞給IO對象.
6. 若是操做失敗,IO對象拋出boost::system::system_error類型的異常.
參考連接
http://www.boost.org/doc/libs/1_64_0/doc/html/boost_asio.html
http://blog.csdn.net/henreash/article/details/7469707
Coroutines
lua的一段代碼
function foo(a) print("foo", a) return coroutine.yield(2 * a) end co = coroutine.create(function ( a, b ) print("co-body", a, b) local r = foo(a + 1) print("co-body", r) local r, s = coroutine.yield(a + b, a - b) print("co-body", r, s) return b, "end" end) print("main", coroutine.resume(co, 1, 10)) print("main", coroutine.resume(co, "r")) print("main", coroutine.resume(co, "x", "y")) print("main", coroutine.resume(co, "x", "y")) 下面是運行結果 co-body 1 10 //print("co-body", a, b) foo 2 //print("foo", a) main true 4 //print("main", coroutine.resume(co, 1, 10)),resume第一值返回true,第二個值返回上次yield(coroutine.yield(2 * a))的入參 4 co-body r // print("co-body", r) r爲上一次yield的返回值,即這一次resume的入參 "r" main true 11, -9 //print("main", coroutine.resume(co, "r")),resume第一值返回true,第二個值返回上次yield( local r, s = coroutine.yield(a + b, a - b))的入參 11, -9 co-body x y // print("co-body", r, s) r,s爲上一次yield的返回值,即這一次resume的入參 "x",y" main true 10 end //print("main", coroutine.resume(co, "x", "y")), resume第一值返回true,第二個值返回create的返回值b, "end" main false cannot resume dead coroutine //coroutine已經完成
std::apply
template <class F, class Tuple>
constexpr decltype(auto) apply(F&& f, Tuple&& t);
//f是函數,t是一串入參
#include <iostream> #include <tuple> #include <utility> int add(int first, int second) { return first + second; } template<typename T> T add_generic(T first, T second) { return first + second; } auto add_lambda = [](auto first, auto second) { return first + second; }; int main() { // OK std::cout << std::apply(add, std::make_pair(1, 2)) << '\n'; // Error: can't deduce the function type // std::cout << std::apply(add_generic, std::make_pair(2.0f, 3.0f)) << '\n'; // OK std::cout << std::apply(add_lambda, std::make_pair(2.0f, 3.0f)) << '\n'; } //3 //5
參考連接 http://en.cppreference.com/w/cpp/utility/apply