views:

152

answers:

2
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM_THREADS 8

char *messages[NUM_THREADS];

struct thread_data
{
   int thread_id;
   int  sum;
   char *message;
};

struct thread_data thread_data_array[NUM_THREADS];

void *PrintHello(void *threadarg)
{
   int taskid, sum;
   char *hello_msg;
   struct thread_data *my_data;

   sleep(1);
   my_data = (struct thread_data *) threadarg;
   taskid = my_data->thread_id;
   sum = my_data->sum;
   hello_msg = my_data->message;
   printf("Thread %d: %s  Sum=%d\n", taskid, hello_msg, sum);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
//int *taskids[NUM_THREADS];
int rc, t, sum;

sum=0;
messages[0] = "English: Hello World!";
messages[1] = "French: Bonjour, le monde!";
messages[2] = "Spanish: Hola al mundo";
messages[3] = "Klingon: Nuq neH!";
messages[4] = "German: Guten Tag, Welt!"; 
messages[5] = "Russian: Zdravstvytye, mir!";
messages[6] = "Japan: Sekai e konnichiwa!";
messages[7] = "Latin: Orbis, te saluto!";

for(t=0;t<NUM_THREADS;t++) {
  sum = sum + t;
  thread_data_array[t].thread_id = t;
  thread_data_array[t].sum = sum;
  thread_data_array[t].message = messages[t];
  printf("Creating thread %d\n", t);
  rc = pthread_create(&threads[t], NULL, PrintHello, (void *)&thread_data_array[t]);
  if (rc) {
    printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
    }
  }
pthread_exit(NULL);
}

The above piece of code gives me a segmentation fault in linux using gcc

As a beginner I have a few questions in mind regarding threads

  1. If 3 threads are created simultaneously and if they have to exit then the thread that is created first is exitted at the end?
  2. Can the o/p differ every time of a thread creation and termination program and if so why??(I have noticed they do so");
+2  A: 

If 3 threads are created simultaneously and if they have to exit then the thread that is created first is exited at the end?

Answer: there is no guarantee that the first thread should exit first. Once they are created, they are treated as separate entities by the OS and it's up to the OS to decide which will exit first.

Can the o/p differ every time of a thread creation and termination program and if so why? (I have noticed they do so);

Answer: yes they do differ, the reason being the same as explained above.

Vijay Sarathi
+1  A: 

How and when each thread is scheduled is completely upto the OS. There is no way to understand this without digging deep into the OS code.

In most implementations of pthreads (I have not used all, but all I have used).
When the main thread exits all the other threads stop executing and the application quits.

Thus before exiting the main thread should wait for (or kill) all child threads before exiting.
For this we have pthread_join().

for(t=0;t<NUM_THREADS;t++)
{
    void* result;
    pthread_join(threads[t],&result);
}
// Now all the children are dead.

Note the underlying io system is not thread safe.
So if multiple threads write to IO then you may potentially get some interleaving.

Martin York