#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;

    // Detach the thread to let it run independently
    myThread.detach();

    std::cout << "Main function done" << std::endl;

    // Main function exits immediately after detaching the thread
    return 0;
}

Embed on website

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