Running Two Independent Threads

The following program shows two independent threads. Here, by ``independent'', it means they do not share variables. More precisely, each thread runs on their own variables.

Click here to download a copy of this program.

#include  <stdio.h>
#include  <stdlib.h>
#include  <thread.h>

#define   P4(x)   ( (x)*(x)*(x)*(x) )

void  *Armstrong(void *Junk)
{
     int  a, b, c, d;
     int  abcd, a4b4c4d4;
     int  count = 0;

     for (a = 0; a <= 9; a++)
          for (b = 0; b <= 9; b++)
               for (c = 0; c <= 9; c++)
                    for (d = 0; d <= 9; d++) {
                         a4b4c4d4 = P4(a) + P4(b) + P4(c) + P4(d);
                         abcd     = a*1000 + b*100 + c*10 + c;
                         if (a4b4c4d4 == abcd) {
                              count++;
                              printf("The %d-th Armstrong number is %d\n",
                                      count, abcd);
                         }
                    }
     printf("\nThere are %d Armstrong Numbers\n", count);
     thr_exit(0);
}


long  Fibon(long  n)
{
     if (n == 0L || n == 1L)
          return  (1L);
     else
          return  (Fibon(n-1) + Fibon(n-2));
}

void  *Fibonacci(void *Arg)
{
     int   *intPTR = (int *) Arg;
     long  n       = *intPTR;

     printf("\tThe %ld-th Fibonacci number is %ld\n",
             n, Fibon(n));
     thr_exit(0);
}

void  main(int argc,  char *argv[])
{
     thread_t  ID1, ID2;
     size_t    Status1, Status2;
     long      n;

     if (argc == 1) {
          printf("Use %s n\n", argv[0]);
          exit(1);
     }
     n = (long) abs(atoi(argv[1]));

     thr_create(NULL, 0, Armstrong, (void *) NULL, 0, &ID1);
     thr_create(NULL, 0, Fibonacci, (void *) (&n), 0, &ID2);

     thr_join(ID1, 0, (void *) &Status1);
     thr_join(ID2, 0, (void *) &Status2);
}