title: Multithreaded reading/doing things with chars from character array in C
body: <p>I am trying to read a character array that contains the contents of many large files. The character array is going to be quite large, because the files are large, so I want to do it using multithreading (pthread). I want the user to be able to designate how many threads they want to run. I have something working, but increasing the number of threads does nothing to affect performance (i.e. 1 thread finishes just as fast as 10). In fact, it seems to be just the opposite: telling the program to use 10 threads runs much slower than telling it to use 1.</p>
  
  <p>Here is the method for slicing up the character array according to the number of threads the user passes to the program. I know this is wrong, I could use some advice here.</p>
  
  <pre><code>//Universal variables
  int numThreads;
  size_t sizeOfAllFiles; // Size, in bytes, of allFiles
  char* allFiles; // Where all of the files are stored, together
  void *zip(void *nthread);
  void *zip(void *nThread) {
       int currentThread = *(int*)nThread;
       int remainder = sizeOfAllFiles % currentThread;
       int slice = (sizeOfAllFiles-remainder) / currentThread;
  
       // I subtracted the remainder for my testing
       // because I didn't want to worry about whether
       // the char array's size is evenly divisible by numThreads
  
       int i = (slice * (currentThread-1));
       char currentChar = allFiles[i]; //Used for iterating
  
       while(i&lt;(slice * currentThread) &amp;&amp; i&gt;=(slice * (currentThread-1))) {
          i++;
          // Do things with the respective thread's
          // 'slice' of the array.
          .....
      }
  return 0;
  }
  </code></pre>
  
  <p>And here is how I am spawning the threads, which I am almost positive that I am doing correctly:</p>
  
  <pre><code>for (int j = 1; j &lt;= threadNum; j++) {
      k = malloc(sizeof(int));
      *k = j;
      if (pthread_create (&amp;thread[j], NULL, zip, k) != 0) {
          printf("Error\n");
          free(thread);
          exit(EXIT_FAILURE);
      }
    }
  for (int i = 1; i &lt;= threadNum; i++)
       pthread_join (thread[i], NULL);
  </code></pre>
  
  <p>This is all really confusing for me so if I could get some help on this, I'd greatly appreciate it. I specifically am struggling with the slicing part (cutting it up correctly), and with not seeing performance gains by using more than one thread. Thanks in advance.</p>