title: Prime numbers in c language
body: <p>I want to find prime numbers with multithreading and using Sieve of E. function.I write some piece of codes. If the program will run, the user enter a max number and thread number. The program should create threads that given thread number. The program find all prime numbers until the max number. Each thread must check one prime number. </p>
  
  <p>My program doesn't find prime numbers. I write checkPrime function and crossout functions for finding prime numbers efficiently. But it doesn't work. So, I can't check my threads work correctly or not. How can I implement checkPrime function?</p>
  
  <p>There are 3 functions. crossout is for Sieve E. method. checkPrime is for checking is a number prime or not. worker is for thread's function. Each thread must check one prime number.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  
  #define MAX_N 100000000
  #define MAX_THREADS 25
  
  // global values
  int threadNumber;
  int largestNumber;
  int isPrime;
  int nthreads,  // number of threads (not counting main())
      prime[MAX_N + 1],
      n,  // in the end, prime[i] = 1 if i prime, else 0
      nextbase;  // next sieve multiplier to be used
  
  // lock for the shared variable nextbase
  pthread_mutex_t nextbaselock = PTHREAD_MUTEX_INITIALIZER;
  
  void crossout(int a) {
      int i, j, check;
      for (i = 2; i &lt; largestNumber; i++)
          prime[i] = 1;
  
      for (i = a; i &lt; largestNumber;)
          if (prime[i])
              for (j = i; i * j &lt; largestNumber; j++)
                  prime[i * j] = 0;
  
  }
  
  int checkPrime(int a) {
      int i;
      for (i = 2; i &lt;= a; ++i) {
          if (a % i == 0) {
              isPrime = 1;
              return isPrime;
              break;
          } else
              isPrime = 2;
          crossout(a);
          return isPrime;
      }
  }
  
  void*  workerThread(void* t) {
  
      int lim, base;
  
      long i, j;
      long tid;
  
      tid = (long)t;
      printf("Thread %ld starting...\n", tid);
  
      while (1)  {
  
  
          pthread_mutex_lock(&amp;nextbaselock);
          base = nextbase;
          nextbase++;
          // unlock the lock
          pthread_mutex_unlock(&amp;nextbaselock);
          if (base &lt;= lim)  {
  
              if (prime[base])  {
                  checkPrime(base);
                  // log work done by this thread
              }
          }
          if (checkPrime(base) == 2)
              printf("Thread %ld done. Prime = %d\n", tid, base);
          pthread_exit((void*) t);
      }
  
      return NULL;
  }
  
  //main function with two parameters :argc and argv
  int main(int argc, char** argv) {
  
      threadNumber = argv[3];
      largestNumber = argv[1];
  
  
      int i;
  
      pthread_t thread[threadNumber];
      int rc;
      long t;
      void* status;
  
      for (t = 0; t &lt; threadNumber; t++) {
          printf("Main: creating thread %ld\n", t);
          rc = pthread_create(&amp;thread[t], NULL, workerThread, (void*)t);
          if (rc) {
              printf("ERROR; return code from pthread_create() is %d\n", rc);
              exit(-1);
          }
      }
  
      for (t = 0; t &lt; threadNumber; t++) {
          rc = pthread_join(thread[t], (void*)&amp;t);
          if (rc) {
              printf("ERROR; return code from pthread_join() is %d\n", rc);
              exit(-1);
          }
          printf("Main: completed join with thread %ld \n", t);
      }
  }
  </code></pre>