Pthread的简单例子

From Yefu's notes
Jump to: navigation, search

Copied from the class notes.

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

void *printme(i)
int *i;
{
  sleep(1);
  printf("Hi.  I'm thread %d\n", *i);
  pthread_exit(NULL);
}

main()
{
  int i, vals[4];
  pthread_t tids[4];
  void *retval;

  for (i = 0; i < 4; i++) {
    vals[i] = i;
    pthread_create(tids+i, NULL, printme, vals+i);
  }
  return 0;
}

<comments/>