First of all, sorting algorithms are very difficult to parallelize with parallel OpenMP loops. This is due to the fact that the loop counter is not deterministic, but depends on the input setpoints that are read at each iteration.
I don’t think that loop conditions like data[left] <= pivot will work well, since the OpenMP library does not know exactly how to divide the iteration space between the streams.
If you are still interested in parallel sorting algorithms, I suggest you read the literature first to see those algorithms that are really worth implementing because of their scalability. If you just want to learn OpenMP, I suggest you start with simpler algorithms such as bucket-sort , where the number of buckets is well known and does not change often.
As for the example that you are trying to parallelize, while loops are not directly supported by OpenMP, because the number of iterations (the number of loop attempts) is not deterministic (otherwise they can be easily converted to loops). Therefore, iterations cannot be distributed between threads. Also, usually when the loops check the condition using the last result of the iteration. This is called Read-after-Write or true-dependency and cannot be parallelized.
The slowdown problem can be reduced if you try to minimize the number of omp parallel sentences. Also, try moving them out of all your loops. These sentences can create and join additional flows which are used in parallel parts of the code, which is expensive.
You can still synchronize threads inside parallel blocks so that the result is similar. In fact, all threads are waiting for each other at the end of the omp for clause by default, so this makes things even easier.
#pragma omp parallel default(none) firstprivate(right,left) private(i,j) shared(length, pivot, data) { #pragma omp for for(i = 1; i<length-1; i++) { if(data[left] > pivot) { i = length; } else { left = i; } } #pragma omp for for(j=length-1; j > 0; j--) { if(data[right] < pivot) { j = 0; } else { right = j; } } } // end omp parallel