title: Divide and Conquer Algorithm using Multithreading
body: <p>I am working on applying multi-threading on the divide and conquer algorithm so that I create a new thread each time the recursive function is called.
  Here is the code of the algorithm alone(Works perfect):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int MAX_VALUE(int ARRAY[],int LEFT,int RIGHT)
  {
      int MID;
      if(LEFT == RIGHT) return ARRAY[LEFT];
      else
      {
          MID = (LEFT + RIGHT) / 2;
          int U = MAX_VALUE(ARRAY, LEFT, MID);
          int V = MAX_VALUE(ARRAY, MID + 1, RIGHT);
          return (U &gt; V ? U : V);
      }
  }
  
  int main()
  {
      int N,i;
  
      printf("Enter the number of elements : ");
      scanf("%d", &amp;N);
      int BUFFER[N];
  
      printf("Enter Array values : ");
      for(i = 0; i &lt; N; i++)
          scanf("%d", &amp;BUFFER[i]);
  
      printf("Maximum element in Array = %d", MAX_VALUE(BUFFER, 0, N-1));
      return 0;
  }
  </code></pre>
  
  <p>And here is the above code combined with multi-threading(ERROR AT pthread_create: lvalue required as unary ‘&amp;’ operand):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  
  pthread_t THREAD_ID[5];
  
  static int i = 0;
  static int FLAG_FUNCTION_CALLS = 0;
  static int FLAG_THREAD_ID = 0;
  
  void *MAX_VALUE(int ARRAY[],int LEFT,int RIGHT)
  {
      int ERR;
      FLAG_FUNCTION_CALLS++;
  
      int ACTIVE_THREAD_ID = pthread_self();
  
      int MID;
      if(LEFT == RIGHT)
      {
          if(FLAG_THREAD_ID &lt;= 4)
          {
              printf("THREAD[%d] roger that and moving out...",ACTIVE_THREAD_ID);
              pthread_exit(&amp;ARRAY[LEFT]);
          }
          else
          {
              return (void *) ARRAY[LEFT];
          }
  
      }
      else
      {
          MID = (LEFT + RIGHT) / 2;
  
          void *U;
          while(FLAG_FUNCTION_CALLS &lt;= 4)
          {
              FLAG_THREAD_ID++;
              ERR = pthread_create(&amp;(THREAD_ID[FLAG_THREAD_ID]), NULL, &amp;MAX_VALUE(ARRAY, LEFT, MID), NULL);
              if (ERR != 0)
                  fprintf(stderr, "\nFAILED CREATE THREAD!");
              else
                  printf("\n Thread created successfully\n");
          }
          pthread_join(&amp;THREAD_ID[FLAG_THREAD_ID],&amp;U);
  
          void *V;
          while(FLAG_FUNCTION_CALLS &lt;= 4)
          {
              FLAG_THREAD_ID++;
              ERR = pthread_create(&amp;(THREAD_ID[FLAG_THREAD_ID]), NULL, &amp;MAX_VALUE(ARRAY, MID + 1, RIGHT), NULL);
              if (ERR != 0)
                  fprintf(stderr, "\nFAILED CREATE THREAD!");
              else
                  printf("\n Thread created successfully\n");
          }
          pthread_join(&amp;THREAD_ID[FLAG_THREAD_ID],&amp;V);
  
          if(FLAG_THREAD_ID &lt;= 4)
          {
              printf("THREAD[%d] roger that and moving out...",ACTIVE_THREAD_ID);
              pthread_exit(U &gt; V ? &amp;U : &amp;V);
          }
          else
          {
              return (U &gt; V ? (void *)U : (void *)V);
          }
      }
  }
  
  int main()
  {
      int N,i,ERR;
      void *MAXIMUM_VALUE;
  
      printf("\nEnter the number of elements : \n");
      scanf("%d", &amp;N);
      int BUFFER[N];
  
      printf("\nEnter Array values : \n");
      for(i = 0; i &lt; N; i++)
          scanf("%d", &amp;BUFFER[i]);
  
      ERR = pthread_create(&amp;(THREAD_ID[0]), NULL, &amp;MAX_VALUE(BUFFER, 0, N-1), NULL);
      if (ERR != 0)
          fprintf(stderr, "\nFAILED CREATE THREAD!");
      else
          printf("\n Thread created successfully\n");
  
      pthread_join(&amp;THREAD_ID[4],&amp;MAXIMUM_VALUE);
      printf("\n%d\nNumber of time the recursive function is called: %d", (int)MAXIMUM_VALUE,FLAG_FUNCTION_CALLS);
  
      return 0;
  }
  </code></pre>
  
  <p>I also wanted to create just 5 threads and each one will terminate and a new one is created after each function call, then only the 5th thread will continue executing and will return the final maximum value to the main function.</p>
  
  <p>I am new to Operating System in C stuff if anyone could help that would be great. Thank you.  </p>