Untitled

 avatar
unknown
c_cpp
2 days ago
3.7 kB
3
Indexable
1 code"
#include <iostream>
#include <thread>
#include <vector>
#include <numeric>
#include <sched.h>

using namespace std;
constexpr int ARRAY_SIZE = 1000000; 
void additionOperation(const vector<int>& array) {
    int core = sched_getcpu();
    cout << "Addition operation is running on core: " << core << endl; 
    long long sum = accumulate(array.begin(), array.end(), 0LL); 
    cout << "Sum of the array elements: " << sum << endl;
}
void multiplicationOperation(const vector<int>& array) {
    int core = sched_getcpu();
    cout << "Multiplication operation is running on core: " << core << endl;   
    long long product = 1LL; 
    for (int num : array) {
        product *= num;
    }
    cout << "Product of the array elements: " << product << endl;
}
int main() {
    vector<int> array(ARRAY_SIZE, 1); 
    thread additionThread(additionOperation, ref(array));
    thread multiplicationThread(multiplicationOperation, ref(array));
    additionThread.join();
    multiplicationThread.join();
    return 0;
}
i will give you 6 codes , then you have to make world document in proper lab report format as writing  code name , code , then output , give me link to download that world file , here are the codes :
2 code :
#include <iostream>
#include <thread>

void addSeven(long x, long* result) {
    *result = x + 7;
}

void addTwoNumbers(long x, long y, long* sum) {
    *sum = x + y;
}

void addThreeNumbers(long x, long y, long z, long* sum) {
    *sum = x + y + z;
}

int main() {
    long a = 10;
    long b = 15, c = 20;
    long x = 2, y = 5, z = 30;
    long result1, result2, result3;

    std::thread th1(addSeven, a, &result1);
    std::thread th2(addTwoNumbers, b, c, &result2);
    std::thread th3(addThreeNumbers, x, y, z, &result3);

    th1.join();
    th2.join();
    th3.join();

    std::cout << result1 << " " << result2 << " " << result3 << "\n";
    return 0;
}
3 code :
#include <iostream>
#include <thread>

int main() {
    std::cout << std:: thread::hardware_concurrency() << "\n";
    return 0;
}
4 code :
#include <iostream>
#include <thread>

void addSeven(long x, long* result) {
    *result = x + 7;
}

void addTwoNumbers(long x, long y, long* sum) {
    *sum = x + y;
}

void addThreeNumbers(long x, long y, long z, long* sum) {
    *sum = x + y + z;
}

int main() {
    long a = 10;
    long b = 15, c = 20;
    long x = 2, y = 5, z = 30;
    
    long result1, result2, result3;
    
    std::thread th1(addSeven, a, &result1);
    std::thread th2(addTwoNumbers, b, c, &result2);
    std::thread th3(addThreeNumbers, x, y, z, &result3);
    
    th1.join();
    th2.join();
    th3.join();
    
    std::cout << result1 << " " << result2 << " " << result3 << "\n";
    
    return 0;
}
5 code :
#include <iostream>
#include <thread>

void printMessage(long i) {
    std::cout << "I am thread " << i << "\n";
}

int main() {
    long numThreads = 5;
    std::thread allTh[numThreads];

    for (long i = 0; i < numThreads; ++i) {
        allTh[i] = std::thread(printMessage, i);
    }

    for (long i = 0; i < numThreads; ++i) {
        allTh[i].join();
    }

    return 0;
}
6 code :
#include <iostream>
#include <thread>

void memoryRace(long* aR) {
    *aR = 0;
    for (long i = 0; i < 100000; ++i) {
        *aR = *aR + 1;
    }
}

int main() {
    std::thread thArray[5];
    long result = 0;

    for (long i = 0; i < 5; ++i) {
        thArray[i] = std::thread(memoryRace, &result);
    }

    for (long i = 0; i < 5; ++i) {
        thArray[i].join();
    }

    std::cout << result << std::endl;
    return 0;
}
Editor is loading...
Leave a Comment