Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
699 B
3
Indexable
Never
#include <iostream>
#include <cmath>
#include <thread>
#include <mutex>
using namespace std;

void faktoriali(int numri, mutex& mtx)
{
    int rezultati = 1;
    int i;
    for (i = 2; i <= numri; i++)         
    {
        rezultati = rezultati * i;  
    }
    
    mtx.lock();
    cout << "Faktoriali i numrit " << numri << " është " << rezultati << endl;
    mtx.unlock();
}

int main ()
{
    int numrat[4] = {5, 4, 3, 2};
    thread threads[4];
    mutex mtx;
    int i;
    for (i = 0; i < 4; i++)
    {
        threads[i] = thread(faktoriali, numrat[i], ref(mtx));
    }
    for (i = 0; i < 4; i++)
    {
        threads[i].join();
    }
    return 0;
}
Leave a Comment