Untitled

 avatar
unknown
c_cpp
2 months ago
938 B
4
Indexable
#include <iostream>
#include <thread>
#include <unistd.h>
#include <sched.h>

using namespace std;

void function1(){
    cout << "Thread ID (function1): " << this_thread::get_id() << " running on core: " << sched_getcpu() << endl;
    for (int i = 0; i < 20000000; i++) {
        cout << "+";  
    }
}

void function2(){
    cout << "Thread ID (function2): " << this_thread::get_id() << " running on core: " << sched_getcpu() << endl;
    for (int i = 0; i < 20000000; i++) {
        cout << "-"; 
    }
}

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