title: Parallel sorting in quick sort using multi threads in C
body: <p>I am just getting acquainted with parallel programming using multi threads. I am trying to implement quick sort using 8 cores. And I am having some problems, I don't know how to correctly implement parallel programing in quicksort.</p>
  
  <p>I fill the array correctly using multi threads, but I can't make it for work quick sort. </p>
  
  <p>I would appreciate any help you can provide. Thank you in advance. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #define KILO (1024)
  #define MEGA (1024*1024)
  #define MAX_ITEMS (64*MEGA)
  
  #define THREADS 8
  #define swap(v, a, b) {unsigned tmp; tmp=v[a]; v[a]=v[b]; v[b]=tmp;}
  
  static int *v;
  static pthread_t pids[THREADS];
  static int args[THREADS];
  
  struct info {
      int low;
      int * data_set ;
      int high;
  };
  
  static void print_array(void) {
      for (int i = 0; i &lt; MAX_ITEMS; i++)
          printf("%d ", v[i]);
      printf("\n");
  }
  
  static void init_array_line(int *line) {
      int i, max;
      i = *line;
      max = i + MAX_ITEMS;
      v = (int *) malloc(MAX_ITEMS * sizeof(int));
      for (i; i &lt; max; i++)
          v[i] = rand() % 10000 + 1;
  }
  
  static void init_array() {
      int i;
      for (i = 0; i &lt; THREADS; i++) {
          pthread_create(pids + i, NULL, &amp;init_array_line, args + i);
      }
  }
  
  static unsigned partition(int *v, unsigned low, unsigned high,  unsigned pivot_index) {
      if (pivot_index != low)
          swap(v, low, pivot_index);
  
      pivot_index = low;
      low++;
  
      while (low &lt;= high) {
          if (v[low] &lt;= v[pivot_index])
              low++;
          else if (v[high] &gt; v[pivot_index])
              high--;
          else
              swap(v, low, high);
      }
  
      if (high != pivot_index)
          swap(v, pivot_index, high);
      return high;
  }
  
  static void quick_sort(void *data){
      struct info *info = data;
      struct info *info1 = data;
  
      int low = info-&gt;low;
      int high = info-&gt;high;
      pthread_attr_t attr;
      pthread_t thread_id1;
      pthread_attr_init(&amp;attr);
      unsigned pivot_index;
  
      if (low &gt;= high)
          return;
  
      pivot_index = (low + high) / 2;
      pivot_index = partition(v, low, high, pivot_index);
      info1-&gt;data_set = info-&gt;data_set;
  
      if (low &lt; pivot_index) {
          info1-&gt;low = low;
          info1-&gt;high = pivot_index - 1;
          pthread_create(pids, NULL, &amp;quick_sort, &amp;info);
      }
  
      if (pivot_index &lt; high) {
          info-&gt;low = pivot_index + 1;
          info-&gt;high = high;
          pthread_create(pids, NULL, &amp;quick_sort, &amp;info);
      }
  
  }
  
  static wait_all() {
      int i;
      for (i = 0; i &lt; THREADS; i++) {
          pthread_join(pids[i], NULL);
      }
  }
  
  int main(int argc, char **argv) {
      init_array();
      wait_all();
  
      struct info *data1 = new struct info ();
      data1-&gt;low = 0;
      data1-&gt;high = MAX_ITEMS - 1;
      data1-&gt;data_set = v;
  
      quick_sort(data1);
      wait_all();
  }
  </code></pre>
  
  <p>I have tried to make it work like in here:</p>
  
  <p><a href="https://stackoverflow.com/questions/32567700/writing-a-parallel-quick-sort-in-c">Writing a parallel quick sort in c</a></p>
  
  <p>But It hasn't worked. </p>