diff --git a/src/space.c b/src/space.c
index dff22dba2f57d072a8a760371cad4db19586b66a..73544daf7a2e26ebdf533ad57d3929185933885a 100644
--- a/src/space.c
+++ b/src/space.c
@@ -596,6 +596,7 @@ void space_do_parts_sort() {
         if (jj > i && pivot > min) {
           qid = atomic_inc(&space_sort_struct.last) %
                 space_sort_struct.stack_size;
+          while (!space_sort_struct.stack[qid].ready);
           space_sort_struct.stack[qid].i = i;
           space_sort_struct.stack[qid].j = jj;
           space_sort_struct.stack[qid].min = min;
@@ -619,6 +620,7 @@ void space_do_parts_sort() {
         if (pivot + 1 < max) {
           qid = atomic_inc(&space_sort_struct.last) %
                 space_sort_struct.stack_size;
+          while (!space_sort_struct.stack[qid].ready);
           space_sort_struct.stack[qid].i = jj + 1;
           space_sort_struct.stack[qid].j = j;
           space_sort_struct.stack[qid].min = pivot + 1;
@@ -1175,4 +1177,4 @@ void space_init(struct space *s, double dim[3], struct part *parts, int N,
 
   /* Build the cells and the tasks. */
   space_regrid(s, h_max, verbose);
-}
+}
\ No newline at end of file