Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!