samedi 25 avril 2015

Is my function thread-safe and reentrant?


I have a function that is called by two threads each having a local copy of a vector. My Assumption was that since each thread has a different vector the function below is thread-safe.
Is the below function thread-safe and reentrant?

int Partition(int high, int low, int pivot, std::vector<int>& arr)
{
    int j = low;
    for (int i = low ; i <= high ; i++)
    {
        if (arr[i] <= pivot)
        {

            swap(i , j++ , arr);

        }
    }
    return arr.size() - j;
}

void swap(int fromIndex , int toIndex , std::vector<int> &arr)
{
    arr[fromIndex] = arr[fromIndex]^arr[toIndex];
    arr[toIndex] = arr[fromIndex]^arr[toIndex];
    arr[fromIndex] = arr[fromIndex]^arr[toIndex];
}


Aucun commentaire:

Enregistrer un commentaire