/∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗ FILE: hello arg3.c

∗ DESCRIPTION:

∗ This ”hello world” Pthreads program demonstrates an unsafe (incorrect)

∗ way to pass thread arguments at thread creation. In this case, the

∗ argument variable is changed by the main thread as it creates new threads. ∗

∗ LAST REVISED: 09/04/02 Blaise Barney ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/

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

#define NUM THREADS 8

char ∗messages[NUM THREADS];

void ∗PrintHello(void ∗threadid) {

int ∗id ptr, taskid;

sleep(3);

id ptr = (int ∗) threadid;

taskid = ∗id ptr;

printf("Thread %d: %s \n", taskid, messages[taskid]); pthread exit(NULL);

}

int main(int argc, char ∗argv[]) {

pthread t threads[NUM THREADS];

int rc, t;

   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++)

{

      printf("Creating thread %d\n", t);

rc = pthread create(&threads[t], NULL, PrintHello, (void ∗) &t); if (rc)

{

   printf("ERROR; return code from pthread create() is %d\n", rc);

exit(−1); }

}

pthread exit(NULL); }