-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy path120 Packaged Tasks.cpp
More file actions
49 lines (38 loc) · 810 Bytes
/
Copy path120 Packaged Tasks.cpp
File metadata and controls
49 lines (38 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <thread>
#include <future>
#include <cmath>
#include <iomanip>
using namespace std;
double calculate_pi(int terms)
{
double sum = 0.0;
if (terms < 1)
{
throw runtime_error("Terms cannot be less than 1");
}
for (int i = 0; i < terms; i++)
{
int sign = pow(-1, i);
double term = 1.0 / (i * 2 + 1);
sum += sign * term;
}
return sum * 4;
}
int main()
{
packaged_task<double(int)> task1(calculate_pi);
future<double> future1 = task1.get_future();
thread t1(move(task1), 0);
try
{
double result = future1.get();
cout << setprecision(15) << result << endl;
}
catch (exception &e)
{
cout << "ERROR! " << e.what() << endl;
}
t1.join();
return 0;
}