Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
926 B
1
Indexable
Never
#include <iostream>
#include <cmath>
#include <thread>
#include <mutex>
using namespace std;

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

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