Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 0.69 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define NUM_THREADS     1000
  5.  
  6. void *PrintHello(void *threadid)
  7. {
  8.    long tid;
  9.    tid = (long)threadid;
  10.    printf("Hello World! It's me, thread #%ld!\n", tid);
  11.    pthread_exit(NULL);
  12. }
  13.  
  14. int main (int argc, char *argv[])
  15. {
  16.    pthread_t threads[NUM_THREADS];
  17.    int rc;
  18.    long t;
  19.    for(t=0; t<NUM_THREADS; t++){
  20.       printf("In main: creating thread %ld\n", t);
  21.       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
  22.       if (rc){
  23.          printf("ERROR; return code from pthread_create() is %d\n", rc);
  24.          exit(-1);
  25.       }
  26.    }
  27.  
  28.    /* Last thing that main() should do */
  29.    pthread_exit(NULL);
  30. }