← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 14 of 17

C++ में async और future

async background में एक काम शुरू करता है, और एक future एक ticket है जो आप बाद में result collect करने के लिए इस्तेमाल करते हैं, एक bakery पर एक claim ticket की तरह। आप जब answer तैयार हो तो इसे pick up कर सकते हैं।
Syntax
cpp
#include <future>

std::future<result_type> future_name = std::async(function_name, arguments);

future_name.get();  // waits and returns the result

std::async क्या है?

'std::async' function template एक task को asynchronously चलाने के लिए इस्तेमाल होता है, potentially एक नए thread में। यह एक 'std::future' object return करता है, जो task के complete होने पर इसकी return value hold करेगा।

उदाहरण: What is std::async?

cpp
#include <iostream>
#include <future>

int compute() { return 42; }

int main() {
	std::future<int> result = std::async(compute); // runs, potentially on a new thread
	std::cout << result.get() << std::endl;
	return 0;
}

std::future के साथ काम करना

एक 'std::future' एक ऐसी value के लिए एक placeholder की तरह act करता है जो calculate हो रही है। एक future पर get() call करना आपके program को block करता है और calculation खत्म होने का wait करता है। आप किसी भी future object पर get() सिर्फ एक बार call कर सकते हैं।

उदाहरण: Working with std::future

cpp
#include <iostream>
#include <future>

int compute() { return 100; }

int main() {
	std::future<int> f = std::async(compute);
	std::cout << f.get() << std::endl; // blocks until the value is ready
	return 0;
}

Launch Policies

आप एक launch policy pass करके control कर सकते हैं कि std::async कैसे चलता है। 'std::launch::async' task को तुरंत एक नए thread पर चलने के लिए force करता है। 'std::launch::deferred' task को तब तक delay करता है जब तक आप get() या wait() call न करें।

उदाहरण: Launch Policies

cpp
#include <iostream>
#include <future>

int compute() { return 5; }

int main() {
	auto f = std::async(std::launch::async, compute); // forces a new thread immediately
	std::cout << f.get() << std::endl;
	return 0;
}

std::promise और std::future

एक 'std::promise' दूसरे thread में चल रहे एक 'std::future' को values भेजने के लिए इस्तेमाल होता है। promise communication channel का input end है, और future output end है।

उदाहरण: std::promise and std::future

cpp
#include <iostream>
#include <future>
#include <thread>

void setValue(std::promise<int> p) {
	p.set_value(7); // sends the value to the paired future
}

int main() {
	std::promise<int> p;
	std::future<int> f = p.get_future();
	std::thread t(setValue, std::move(p));
	std::cout << f.get() << std::endl;
	t.join();
	return 0;
}

Multiple Futures पर Wait करना

आप एक asynchronous task के खत्म होने का wait करने के लिए, एक specified timeout limit तक, wait_for() इस्तेमाल कर सकते हैं। यह आपके program को अगर एक task hang हो जाए तो indefinitely block होने से रोकता है।

उदाहरण: Waiting on Multiple Futures

cpp
#include <iostream>
#include <future>
#include <chrono>

int compute() { return 1; }

int main() {
	auto f = std::async(compute);
	if (f.wait_for(std::chrono::seconds(1)) == std::future_status::ready) { // avoids blocking forever
		std::cout << f.get() << std::endl;
	}
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. future पर get() call न करना, इसलिए result या exception कभी इस्तेमाल नहीं होता।
  2. यह न जानना कि default launch policy task deferred चला सकती है।
  3. यह उम्मीद करना कि exceptions async को call करते वक्त throw होंगे, get() पर नहीं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.