Modernes C++ programmieren

Okt 23, 2024

lst-0883-book.cpp

// https://godbolt.org/z/dz75jr1rM 
#include <iostream>
#include <thread>
#include <syncstream>
long fib(long n) { return n<=1 ? n : fib(n-1)+fib(n-2); }
void runFib(long von, long schritt, long bis) {
    for (auto n=von; n<=bis; n+=schritt) {
        std::osyncstream osync{ std::cout }; // Sync auf cout, solange osync existiert
        osync << "fib("<<n<<")=" << fib(n) << '\n';
    }
}
int main() {
    std::jthread f40{ runFib, 1, 3, 40 };
    std::jthread f41{ runFib, 2, 3, 41 };
    std::jthread f42{ runFib, 3, 3, 42 };
}