#include <iostream>
#include <thread>
#include <chrono>

// Function that will be executed by the thread
void continuousThreadFunction() {
    auto start = std::chrono::steady_clock::now();
    while (std::chrono::steady_clock::now() - start < std::chrono::seconds(10)) {
        std::cout << "Thread is running continuously..." << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1)); // Sleep for 1 second
    }
    std::cout << "Thread finished its 20-second run." << std::endl;
}

int main() {
    // Create a thread that runs the continuousThreadFunction
    std::thread myThread(continuousThreadFunction);

    // Main function executes its own code
    std::cout << "Main function is running..." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(3)); // Simulate some work in the main function

    std::cout << "Main function finished initial work" << std::endl;

    // Wait for the thread to complete
    if (myThread.joinable()) {
        myThread.join();
    }

    // Continue with additional tasks after the thread has finished
    std::cout << "Main function resumed after thread completion" << std::endl;

    // Main function can now exit or perform other tasks
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: