title: Trying to understand Race Conditions/Threads in C
body: <p>For staters, I am a student who wasn't a CS undergrad, but am moving into a CS masters. So I welcome any and all help anyone is willing to give.</p>
  
  <p>The purpose of this was to create N threads between 2-4, then using a randomly generated array of lower case characters, make them uppercase.</p>
  
  <p>This needed to be done using the N threads (defined by the command line when executed), dividing the work up as evenly as possible, using pthread.</p>
  
  <p>My main question I'm trying to ask, is if I avoided race conditions between my threads? </p>
  
  <p>I am also struggling to understand dividing the work among the threads. As I understand (correct me if I'm wrong), in general the threads functioning will be chosen at random during execution. So, I'm assuming I need to do something along the lines of dynamically dividing the array among the N number of threads and setting it so that each thread will perform the uppercasing of a same sized subsection of the array?  </p>
  
  <p>I know there are likely a number of other discrepancies I need to get better at within my code, but I haven't coded long and just started using C/C++ about a month ago. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;ctype.h&gt;
  
  //Global variable for threads
  char randChars[60];
  int j=0;
  
  //Used to avoid race conditions
  pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  
  //Establish the threads
  void* upperThread(void* argp)
  {
      while(randChars[j])
      {
          pthread_mutex_lock( &amp;mutex1 );
          putchar (toupper(randChars[j]));
          j++;
          pthread_mutex_unlock( &amp;mutex1 );
      }
  
  return NULL;
  
  }
  
  int main(int argc, char **argv)
  {
      //Initializae variables and thread
      int N,randNum,t;
      long i;
      pthread_t pth[N];
      pthread_mutex_init(&amp;mutex1, NULL);
      char randChar = ' ';
  
      //Check number of command inputs given
      if(argc!=2)
      {
          fprintf(stderr,"usage: %s &lt;enter a value for N&gt;\n", argv[0]);
          exit(0);
      }
  
      N = atoi(argv[1]);
  
      //Checks command inputs for correct values
      if(N&lt;2||N&gt;4){
          printf("Please input a value between 2 and 4 for the number of threads.\n");
          exit(0);
      }
  
      //Seed random to create a randomized value
      srand(time(NULL));
      printf("original lower case version:\n");
  
      for (i=0; i&lt;61; i++)
      {
          //Generate a random integer in lower alphabetical range
          randNum = rand()%26;
          randNum = randNum+97;
  
          //Convert int to char and add to array
          randChar = (char) randNum;
          randChars[i] = randChar;
          printf("%c", randChar);
      }
  
      //Create N threads
      for (i=0; i&lt;N; i++)
      {
          pthread_create(pth + i, NULL, upperThread, (void *)i);
      }
  
      printf("\n\nupper case version:\n");
  
      //Join the threads
      for(t=0; t &lt; N; t++)
      {
          pthread_join(pth[t], NULL);
      }
      printf("\n");
  
      pthread_exit(NULL);
  
      return 0;
  }
  </code></pre>