samedi 30 mai 2015

How to compare thread's result with other variable

The thing is I have one function that calculates and gets result of these operations. After getting result I want to compare and get the best result using criteria. The problem is that I do not know if it is good idea to compare variable with result got by threads. Is it going to mess up things for example if two threads are comparing with this variable at the same time or it has some sort of defense?

#include <iostream>
#include <thread>

double RESULT;

void call_from_thread(int i)
{
    //some calculation
    double result;
    if(result>RESULT)
    {
        RESULT=result;
    }
}

int main(int argc, char *argv[])
{
    std::thread t[64];
    for(int i=0;i<64;i++)
    {
        t[i]=std::thread(call_from_thread,i);
    }

    for(int i=0;i<64;i++)
    {
        t[i].join();
    }


    return 0;
}

Is it going to mess RESULT?

Aucun commentaire:

Enregistrer un commentaire