Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <vector>
- void fibonacci(int n, int& result) {
- if (n <= 1) {
- result = n;
- } else {
- int a, b;
- std::thread t1(fibonacci, n - 1, std::ref(a));
- std::thread t2(fibonacci, n - 2, std::ref(b));
- t1.join();
- t2.join();
- result = a + b;
- }
- }
- int main() {
- int n = 10; // Calculate Fibonacci sequence up to the 10th number
- int result;
- fibonacci(n, result);
- std::cout << "Fibonacci sequence up to " << n << "th number is: " << result << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement