C++11 thread library and std::async In Hindi

C++11 thread library and std::async In Hindi

C++ प्रोग्रामिंग लैंग्वेज में मल्टीथ्रेडिंग प्रोग्रामिंग कांसेप्ट को सपोर्ट और इनेबल करने के लिए C++ थ्रेड क्लास प्रोग्राम लाइब्रेरी को लांच किया गया था, और यह एक सिंगल क्लास प्रोग्राम में मल्टी-टास्किंग सिंगल टाइम में मल्टीप्ल क्लास थ्रेड डेवलपमेंट के फीचर्स में से एक std::async क्लास लाइब्रेरी है। std::async थ्रेड क्लास लाइब्रेरी C++ यूजर को एसिंक्रोनस टास्क इनेबल करता है, इसका मतलब है कि ये ऐसे थ्रेड टास्क है, जो बाकी प्रोग्राम के साथ एक साथ रन होते हैं, बिना प्रॉपर आर्डर में थ्रेड्स को मैनेज करने की ज़रूरत के इसे कर सकते है।

C++11 thread library and stdasync In Hindi

Overview of the Thread Library in the C++11 version of C++ Programming.

C++ प्रोग्रामिंग C++11 वर्जन में थ्रेड लाइब्रेरी किसी सिंगल प्रोग्राम में मल्टीथ्रेडिंग टास्क के लिए कई कंपोनेंट प्रोवाइड करती है, जिनमें मुख्य रूप से यूज़ होने वाले कई एलिमेंट शामिल होते हैं.

Elements of the Thread Library in the C++11 version.

  • std::thread – इस थ्रेड क्लास लाइब्रेरी को किसी C++ प्रोगाम में थ्रेड्स क्रिएट और मैनेज करने के लिए यूज़ किया जाता है।
  • std::mutex – इस थ्रेड क्लास लाइब्रेरी को किसी C++ प्रोगाम में म्यूचुअल एक्सक्लूज़न के लिए (डेटा रेस को अवॉयड करने के लिए थ्रेड क्लास एलिमेंट रिसोर्स को लॉक करने) में यूज़ किया जाता है।
  • std::lock_guard – इस थ्रेड क्लास लाइब्रेरी को किसी C++ प्रोगाम में म्यूटेक्स डेटा को सिक्योर करने और बाद में म्यूटेक्स डेटा को रिलीज़ करने के लिए यूज़ किया जाता है।
  • std::condition_variable – इस थ्रेड क्लास लाइब्रेरी को किसी C++ प्रोगाम में कुछ पर्टिकुलर कंडीशन के आधार पर अन्य प्रोग्राम थ्रेड्स का वेट करने और उन्हें नोटिफ़ाई करने में यूज़ किया जाता है।
  • std::async – इस थ्रेड क्लास लाइब्रेरी को किसी C++ प्रोगाम में एसिंक्रोनस टास्क को लॉन्च करने में यूज़ किया जाता है।
  • std::future – इस थ्रेड क्लास लाइब्रेरी को किसी C++ प्रोगाम में एसिंक्रोनस ऑपरेशन से रिज़ल्ट को डिस्प्ले करने में यूज़ किया जाता है।

So, let’s learn in detail about std::async in C++, which is one of the most powerful features for managing asynchronous thread class operations in C++ programs.

std::async Thread Class Overview Concept in C++.

C++ थ्रेड क्लास प्रोगाम में std::async यूजर डिफाइन टास्क को एसिंक्रोनस आर्डर में  रन करने के लिए एक हायर-लेवल इंटरफ़ेस प्रोवाइड करता है। यह रियल टाइम में एक नया थ्रेड क्रिएट करता है, (या किसी और मेथड से थ्रेड टास्क को एसिंक्रोनस आर्डर में रन करता है) और एक std::future ऑब्जेक्ट वैल्यू को रिटर्न करता है. जिसका यूज़ कैलकुलेशन कम्पलीट होने के बाद उसका रिज़ल्ट डिस्प्ले करने में किया जा सकता है।

Syntax of the std::async Thread class.

std::future<T> std::async(std::launch::policy, callable, arguments…);

Element of the std::async Thread class.

  • std::launch::policy – यह C++ थ्रेड क्लास प्रोगाम में फिक्स करता है कि यूजर डिफाइन थ्रेड क्लास टास्क एसिंक्रोनस आर्डर में (एक अलग थ्रेड में) रन किया जाए या डेफर्ड (ज़रूरत पड़ने पर रन करने के लिए) यूज़ किया जा सकता है।
  • std::launch::async – यह C++ थ्रेड क्लास प्रोगाम में टास्क को एक अलग थ्रेड में रन करने के लिए फाॅर्स करता है।
  • std::launch::deferred – यह C++ थ्रेड क्लास प्रोगाम में टास्क को तब तक नहीं रन करता है, जब तक की उसमे रिज़ल्ट की ज़रूरत न हो (जैसे, get() फंक्शन को फ्यूचर में कॉल किया जाता है)।

C++ यूजर std::launch::async | std::launch::deferred में भी थ्रेड क्लास को पास कर सकते हैं. जिससे की आपका सिस्टम यह फिक्स कर सके कि थ्रेड टास्क को एसिंक्रोनसली रन करना है, या डेफर्ड करना है।

Element of std::launch::async | std::launch::deferred.

  • callable – यह C++ थ्रेड क्लास प्रोगाम में एक फ़ंक्शन, लैम्ब्डा, या कोई भी कॉलेबल ऑब्जेक्ट मेथड है।
  • arguments… – यह C++ थ्रेड क्लास प्रोगाम में कॉलेबल ऑब्जेक्ट को पास करने के लिए आर्गुमेंट्स एलिमेंट है।

Basic usage example of std::async in a C++ thread.

#include <iostream>

#include <thread>

#include <future>

int countTotal(int p, int q) {

    return p + q;

}

int main() {

    // here we launch an asynchronous task thread method

    std::future<int> output = std::async(std::launch::async, countTotal, 7, 4);

    // here you can do other work while the task is running in the background process

    std::cout << “Here main thread is open to do another task.” << std::endl;

    // here we can get the output from the async thread task

    std::cout << “The output of countTotal thread is – ” << output.get() << std::endl;

    return 0;

}

Explanation of usage example of std::async in a C++ thread.

  • यहाँ इस एक्साम्प्ल में, std::async(std::launch::async, countTotal, 7, 4); एक अलग थ्रेड में एसिंक्रोनसली countTotal फंक्शन को लॉन्च करता है।
  • इसमें std::future<int> ऑब्जेक्ट वैल्यू आउटपुट रिटर्न होता है, जो कैलकुलेशन टर्मिनेट होने के बाद उसके आउटपुट वैल्यू को होल्ड करेगा।
  • आउटपुट डिस्प्ले करने के लिए output.get() फंक्शन को कॉल किया जाता है, जो टास्क कम्पलीट होने तक ब्लॉक रहता है, और कैलकुलेट की गई वैल्यू को रिटर्न करता है।

याद रहे, यहाँ get() फंक्शन मौजूदा प्रोग्राम में तब तक ब्लॉक रहता है, जब तक एसिंक्रोनस टास्क कम्पलीट नहीं हो जाता और आउटपुट रिजल्ट अवेलेबल नहीं हो जाता है।

Deferred execution with std::async in a C++ thread.

C++ यूजर थ्रेड में डेफ़र्ड एग्ज़िक्यूशन के साथ std::async थ्रेड क्लास लाइब्रेरी को भी अप्लाई कर सकते हैं, इसका मतलब है कि मौजूदा थ्रेड टास्क इमीडियेट स्टार्ट नहीं होगा। इसके बदले, यह तब एग्ज़िक्यूट होगा जब C++ यूजर रिटर्न किए गए std::future पर get() फंक्शन को कॉल करेंगे।

Example of deferred execution in a C++ thread.

#include <iostream>

#include <thread>

#include <future>

int countTotal(int p, int q) {

    std::cout << “Here it countTotal in the background thread” << std::endl;

    return p + q;

}

int main() {

    // here we launch an asynchronous thread task with deferred execution method

    std::future<int> output = std::async(std::launch::deferred, countTotal, 9, 8);

    // here the task has not yet started, it will only start when we call get() function method

    std::cout << “Implementing another task in main thread” << std::endl;

    // here the task is executed when we call output.get() function method

    std::cout << “Output of countTotal thread is – ” << output.get() << std::endl;

    return 0;

}

Explanation of deferred execution in a C++ thread.

  • यहाँ इस एक्साम्प्ल में, std::async(std::launch::deferred, …) यह इस प्रोग्राम में एक डेफर्ड एसिंक्रोनस टास्क क्रिएट करता है। इसमें थ्रेड टास्क इमीडियेट एग्जीक्यूट नहीं होता है. यह तभी रन होगा, जब रिटर्न किए गए std::future पर get() फंक्शन को कॉल किया जाएगा।
  • यहाँ C++ यूजर देख सकते हैं कि जब तक get() फंक्शन मेथड को कॉल नहीं किया जाता है, तब तक इसे कैलकुलेशन नहीं किया जाता है।

Handling exceptions in std::async in a C++ thread.

यदि किसी C++ प्रोगाम में std::async के साथ लॉन्च किया गया फ़ंक्शन कोई एक्सेप्शन एरर को थ्रो करता है. तो C++ यूजर std::future ऑब्जेक्ट पर get() फंक्शन को कॉल करके उस एक्सेप्शन को कैच कर सकते हैं।

Example of exception handling in std::async in a C++ thread.

#include <iostream>

#include <thread>

#include <future>

int countDivision(int p, int q) {

    if (q == 0) {

        throw std::runtime_error(“Value is Division by zero is not allowed”);

    }

    return p / q;

}

int main() {

    // here the launch an asynchronous thread task that may throw an exception

    std::future<int> output = std::async(std::launch::async, countDivision, 7, 0);

    try {

        // here we call get() function method to retrieve the output or throw the exception

        std::cout << “The output of division is – ” << output.get() << std::endl;

    }

    catch (const std::exception& e) {

        std::cout << “Here it caught the thread exception – ” << e.what() << std::endl;

    }

    return 0;

}

Explanation of exception handling in std::async in a C++ thread.

  • यहाँ इस एक्साम्प्ल में, डिवाइड फ़ंक्शन ज़ीरो से डिवीज़न वैल्यू को टेस्ट करता है, और अगर डिनॉमिनेटर ज़ीरो वैल्यू है तो std::runtime_error एक एक्सेप्शन एरर को थ्रो करता है।
  • जब output.get() फंक्शन मेथड को कॉल किया जाता है, तो एक्सेप्शन को एसिंक्रोनस टास्क से फिर से थ्रो किया जाता है, और मेन थ्रेड के कैच ब्लॉक में कैच किया जाता है।

Leave a Reply