C++ में async और future
In this page:
#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?
#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;
}
Login to try C/C++/Java/PHP code in the editor
std::future के साथ काम करना
एक 'std::future' एक ऐसी value के लिए एक placeholder की तरह act करता है जो calculate हो रही है। एक future पर get() call करना आपके program को block करता है और calculation खत्म होने का wait करता है। आप किसी भी future object पर get() सिर्फ एक बार call कर सकते हैं।
उदाहरण: Working with std::future
#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;
}
Login to try C/C++/Java/PHP code in the editor
Launch Policies
आप एक launch policy pass करके control कर सकते हैं कि std::async कैसे चलता है। 'std::launch::async' task को तुरंत एक नए thread पर चलने के लिए force करता है। 'std::launch::deferred' task को तब तक delay करता है जब तक आप get() या wait() call न करें।
उदाहरण: Launch Policies
#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;
}
Login to try C/C++/Java/PHP code in the editor
std::promise और std::future
एक 'std::promise' दूसरे thread में चल रहे एक 'std::future' को values भेजने के लिए इस्तेमाल होता है। promise communication channel का input end है, और future output end है।
उदाहरण: std::promise and std::future
#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;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Futures पर Wait करना
आप एक asynchronous task के खत्म होने का wait करने के लिए, एक specified timeout limit तक, wait_for() इस्तेमाल कर सकते हैं। यह आपके program को अगर एक task hang हो जाए तो indefinitely block होने से रोकता है।
उदाहरण: Waiting on Multiple Futures
#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;
}
Login to try C/C++/Java/PHP code in the editor
- future पर
get()call न करना, इसलिए result या exception कभी इस्तेमाल नहीं होता। - यह न जानना कि default launch policy task deferred चला सकती है।
- यह उम्मीद करना कि exceptions
asyncको call करते वक्त throw होंगे,get()पर नहीं।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced