#include <stdio.h>
#include <pthread.h>

pthread_t t1,t2;  /* thread process descriptor */
int i1,i2;

/**
 * pthread2 : create two threads
 *
 * My first C program
 * No parallel code - just a print
 * on the screen
 * you can called it like hello-world 1 3 4 fsd "et argument"
 *
 * The program thread "main" create two threads, and sleep in 10 seconds
 * before terminating.
 *
 * Each thread loops 9 times and prints loop number and transferred parm
 *
 * Compilation at linux:   gcc -lpthread pthread2.c -o pthread2
 * Compilation at solaris: gcc -lpthread pthread2.c -o pthread2
 * exe file will be pthread 1 or 2
 */

void * thread_body(void * aparm)
{
  int i,j;
  /* Take a copy of the "1/2" which in a dirty way is
   * transferred by a void pointer
   */
  j = (int)aparm;
  for (i=0;i<9; i++)
    {
      printf("%i loop nr %i\n",j,i);
    sleep(j);
    }
}

int main(int argc, char *argv[])
{
  int i;
  int res;
  printf("\nHello out there\n");
  printf("I have been called with %i parms\n", argc);

  printf("creating a thread NOW\n");

  /* create thread nr 1 and and transfer "1" to the thread */
  res = pthread_create(&t1,NULL,thread_body,(void *)1);

  /* create thread nr 1 and and transfer "2" to the thread */
  res = pthread_create(&t2,NULL,thread_body,(void *)2);

  for (i=0;i<argc;i++) 
  {
    printf("arg %i: %s\n",i,argv[i]);
  }
  sleep(10);
}
