开发者

Pthreads problem and a few questions

#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开发者_如何学Python!";

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");


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.


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.


Your code is working perfectely(I am also using gcc)..

output:

Creating thread 0
Creating thread 1
Creating thread 2
Creating thread 3
Creating thread 4
Creating thread 5
Creating thread 6
Creating thread 7
Thread 1: French: Bonjour, le monde!  Sum=1
Thread 0: English: Hello World!  Sum=0
Thread 2: Spanish: Hola al mundo  Sum=3
Thread 4: German: Guten Tag, Welt!  Sum=10
Thread 3: Klingon: Nuq neH!  Sum=6
Thread 5: Russian: Zdravstvytye, mir!  Sum=15
Thread 6: Japan: Sekai e konnichiwa!  Sum=21
Thread 7: Latin: Orbis, te saluto!  Sum=28
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜