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()...
    
  • 相关阅读:
    E3-1230和E3-1230 V2有多神?
    自己制作 SPx N合1 自动安装盘(x86)
    ARP防火墙绑定网关MAC地址预防ARP攻击和P2P终结者
    完全备份、差异备份和增量备份的权威解释!!!
    Win7 SP1语言包微软官方下载地址及使用方法 2
    Windows 7 不同安装模式简要区别(图解)
    Windows PE3.0制作方法(从Win7中提取制作)
    摄影初学者挑选相机的常见问题 FAQ
    提升域用户帐户在本地计算机上的权限
    Oracle重置序列
  • 原文地址:https://www.cnblogs.com/mangoczp/p/14893901.html
Copyright © 2011-2022 走看看