码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 11 async. (async/deferred)

时间:2021-06-18 18:48:54      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:_for   read   lis   lob   ocs   executors   class   out   ref   

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()...

c++ 11 async. (async/deferred)

标签:_for   read   lis   lob   ocs   executors   class   out   ref   

原文地址:https://www.cnblogs.com/mangoczp/p/14893901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!