Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. Convert the following program to use async expression. #include #include #include void initiazer(std::promise && promObj) { std::cout < < Inside Thread < < std::endl;
1. Convert the following program to use async expression.
#include#include #include void initiazer(std::promise && promObj) { std::cout << "Inside Thread" << std::endl; promObj.set_value(35); } int main() { std::promise promiseObj; std::future futureObj = promiseObj.get_future(); std::thread th(initiazer, std::move(promiseObj)); th.join(); std::cout << futureObj.get() << std::endl; return 0; }
2. Revise the following swap function to use r-value reference.
void swap(int &a, int &b) { int tmp(a); a = b; b = tmp; }
3.The following program performs a computation of randomly generated values within two threaded functions (sum = val1 * val2 + val3 * val4). Convert the lambda expression as one function definition. For this, you must place the global variables (output1 and output2) as well as the function definition outside of the main function.
#include#include #include using namespace std; int main() { srand(time(NULL)); auto get_rand = []() {return rand() % 10; }; int output1, output2; thread t1 = thread([&](int val1, int val2) { output1 = val1 * val2; cout << val1 << " * " << val2 << "=" << output1 << endl; }, get_rand(), get_rand()); thread t2 = thread([&](int val3, int val4) { output2 = val3 * val4; cout << val3 << " * " << val4 << "=" << output2 << endl; }, get_rand(), get_rand()); t1.join(); t2.join(); int sum = output1 + output2; cout << "sum: " << sum << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started