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\n";

	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\n";

	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()...
posted @ 2021-06-17 16:04  mangoCzp  阅读(158)  评论(0编辑  收藏  举报