zoukankan      html  css  js  c++  java
  • c++ 11 async. (async/deferred)

    The current C++11 std::launch only has two modes: async or deferred. In a production system, neither is what you want: async will launch a new thread for every launch without limit, while deferred will defer the work until it is needed lazily, but then do the work in the current thread synchronously when it is needed.
    https://github.com/facebook/folly/blob/master/folly/docs/Executors.md
    https://en.cppreference.com/w/cpp/thread/async

    Just test it.

    • deferred test:
    #include<iostream>
    #include<future>
    using namespace std;
    
    class ThreadManage {
    public:
    	int myThread(int num) {
    		cout << "Thread start id = " << this_thread::get_id() << endl;
    		chrono::milliseconds time(5000); //sleep 5s
    		this_thread::sleep_for(time);
    		cout << "Thread end id = " << this_thread::get_id() << endl;
    		return num *2;
    	}
    };
    
    int main() {
    	ThreadManage manage;
    	cout << "Main start id = " << this_thread::get_id() << endl;
    	// future<int> result = async(std::launch::async, &ThreadManage::myThread,&manage,5);
    	future<int> result = async(std::launch::deferred, &ThreadManage::myThread,&manage,5);
            chrono::milliseconds time(6000);
            this_thread::sleep_for(time);
    	cout << "continue......" << endl;
    
            auto start = chrono::high_resolution_clock::now();
            auto res = result.get();
            auto end = chrono::high_resolution_clock::now();
            std::chrono::duration<double> diff = end - start;
            cout<<"use "<<diff.count()<<" s
    ";
    
    	cout << "result = " << res << endl;
            cout << "After result.get()..."<<endl;
    
            return 0;
    }
    

    output:

    Main start id = 0x116933dc0
    continue......
    Thread start id = 0x116933dc0
    Thread end id = 0x116933dc0
    use 5.00161 s
    result = 10
    After result.get()...
    

    • async test:
    #include<iostream>
    #include<future>
    using namespace std;
    
    class ThreadManage {
    public:
    	int myThread(int num) {
    		cout << "Thread start id = " << this_thread::get_id() << endl;
    		chrono::milliseconds time(5000); //sleep 5s
    		this_thread::sleep_for(time);
    		cout << "Thread end id = " << this_thread::get_id() << endl;
    		return num *2;
    	}
    };
    
    int main() {
    	ThreadManage manage;
    	cout << "Main start id = " << this_thread::get_id() << endl;
    	// future<int> result = async(std::launch::async, &ThreadManage::myThread,&manage,5);
    	future<int> result = async(std::launch::deferred, &ThreadManage::myThread,&manage,5);
            chrono::milliseconds time(6000);
            this_thread::sleep_for(time);
    	cout << "continue......" << endl;
    
            auto start = chrono::high_resolution_clock::now();
            auto res = result.get();
            auto end = chrono::high_resolution_clock::now();
            std::chrono::duration<double> diff = end - start;
            cout<<"use "<<diff.count()<<" s
    ";
    
    	cout << "result = " << res << endl;
            cout << "After result.get()..."<<endl;
    
            return 0;
    }
    

    output:

    Main start id = 0x1132c3dc0
    Thread start id = 0x70000afe6000
    Thread end id = 0x70000afe6000
    continue......
    use 1.935e-05 s
    result = 10
    After result.get()...
    
  • 相关阅读:
    loj#2540. 「PKUWC2018」随机算法
    loj#2538. 「PKUWC2018」Slay the Spire
    loj#2537. 「PKUWC2018」Minimax
    CF662C Binary Table
    bzoj4589: Hard Nim
    【HDU5909】Tree Cutting(FWT)
    P3175 [HAOI2015]按位或
    P4389 付公主的背包
    P4233 射命丸文的笔记
    GFS分布式文件系统环境部署与管理
  • 原文地址:https://www.cnblogs.com/mangoczp/p/14893901.html
Copyright © 2011-2022 走看看