Untitled

 avatar
unknown
plain_text
18 days ago
1.1 kB
4
Indexable
#include <iostream>
#include <thread>
#include <unistd.h>
#include <sched.h>

using namespace std;

void function1() {
    for (int i = 0; i < 20000000; i++) {
        cout << "+";  
    }
    // Print thread ID and core AFTER loop finishes
    cout << "\nThread ID (function1): " << this_thread::get_id() 
         << " finished on core: " << sched_getcpu() << endl;
}

void function2() {
    for (int i = 0; i < 20000000; i++) {
        cout << "-"; 
    }
    // Print thread ID and core AFTER loop finishes
    cout << "\nThread ID (function2): " << this_thread::get_id() 
         << " finished on core: " << sched_getcpu() << endl;
}

int main() {
    time_t start, end;
    time(&start);  
    ios_base::sync_with_stdio(false);  

    thread worker1(function1);
    thread worker2(function2);

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

    time(&end);  

    double time_taken = double(end - start); 
    cout << "\nTime taken by program is: " << fixed << time_taken << setprecision(20) << " sec" << endl;  

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