فراخوانی 2 متد به صورت همزمان - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

فراخوانی 2 متد به صورت همزمان

0 امتیاز
سلام.

فرض کنید دو تا متد دارم و می خوام این 2 متد را به صورت همزمان فراخوانی کنم راحت ترین راه حل چیه؟
سوال شده بهمن 11, 1401  بوسیله ی zirak (امتیاز 473)   3 28 52

1 پاسخ

+1 امتیاز
 
بهترین پاسخ
در C++، با استفاده از تابع std::async از سربرگ <future> می توانید همزمان دو متد را فراخوانی کنید. تابع std::async یک شی std::future را برمی گرداند که نتیجه عملیات ناهمزمان را نشان می دهد.
 
در اینجا مثالی از نحوه فراخوانی دو تابع به طور همزمان آورده شده است:
#include <iostream>
#include <future>
#include <chrono>

void function1() {
    std::cout << "Function 1 started" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(3));
    std::cout << "Function 1 finished" << std::endl;
}

void function2() {
    std::cout << "Function 2 started" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "Function 2 finished" << std::endl;
}

int main() {
    auto future1 = std::async(std::launch::async, function1);
    auto future2 = std::async(std::launch::async, function2);
    
    future1.get();
    future2.get();
    
    return 0;
}

 

در این مثال، function1 و function2 به صورت ناهمزمان با استفاده از std::async راه اندازی می شوند و هر دو به طور همزمان شروع به اجرا می کنند. متد get در اشیاء std::future فراخوانی می شود تا منتظر نتایج توابع باشد.
 
توجه داشته باشید که خط مشی std::launch::async مشخص می کند که تابع باید به صورت ناهمزمان اجرا شود. اگر مشخص نشده باشد، پیاده سازی ممکن است انتخاب کند که تابع را به صورت همزمان اجرا کند.
میشه مثال را یک مقدار کامل تر کرد:
#include <iostream>
#include <future>
#include <functional>
#include <chrono>
#include <memory>

class Example {
public:
    int instanceMethod1(int x, int y) {
        std::cout << "Instance method1 started" << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(3));
        std::cout << "Instance method1 finished" << std::endl;
        return x + y;
    }
    int instanceMethod2(int x, int y) {
        std::cout << "Instance method2 started" << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(5));
        std::cout << "Instance method2 finished" << std::endl;
        return x + y;
    }
};

int main() {
    auto example = std::make_shared<Example>();
    auto instanceMethod1 = std::bind(&Example::instanceMethod1, example, std::placeholders::_1, std::placeholders::_2);
    auto instanceMethod2 = std::bind(&Example::instanceMethod2, example, std::placeholders::_1, std::placeholders::_2);
    auto future1 = std::async(instanceMethod1, 3, 4);
    auto future2 = std::async(instanceMethod1, 7, 8);
    
    int result1 = future1.get();
    int result2 = future2.get();

    std::cout << "Result1: " << result1 << std::endl;
    std::cout << "Result2: " << result2 << std::endl;
    
    return 0;
}

 

 

پاسخ داده شده بهمن 11, 1401 بوسیله ی farnoosh (امتیاز 8,362)   20 44 59
انتخاب شد بهمن 20, 1401 بوسیله ی zirak
...