Untitled

 avatar
unknown
plain_text
9 days ago
875 B
2
Indexable
#include <iostream>
#include <thread>
#include <mutex>
#include <unistd.h>
#include <sched.h>

std::mutex cout_mutex;

void function1() {
    for (int i = 0; i < 20000000; i++) {
        // Perform work
    }
    std::lock_guard<std::mutex> lock(cout_mutex);
    std::cout << "\nThread ID (function1): " << std::this_thread::get_id()
              << " finished on core: " << sched_getcpu() << std::endl;
}

void function2() {
    for (int i = 0; i < 20000000; i++) {
        // Perform work
    }
    std::lock_guard<std::mutex> lock(cout_mutex);
    std::cout << "\nThread ID (function2): " << std::this_thread::get_id()
              << " finished on core: " << sched_getcpu() << std::endl;
}

int main() {
    std::thread worker1(function1);
    std::thread worker2(function2);

    worker1.join();
    worker2.join();

    return 0;
}
Editor is loading...
Leave a Comment