#include <pthread.h>
#include <stdio.h>
#define NUM_THREAD 5
void *print_hello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello world! thread#%ld\n", tid);
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREAD];
int rc;
long i;
for(i =0; i<NUM_THREAD; i++)
{
printf("In main: create thread %ld\n", i);
rc = pthread_create(&threads[i], NULL, print_hello, (void*)i);
if(rc)
{
printf("ERROR: return code from pthread_create() is %d", rc);
exit(1);
}
}
pthread_exit(NULL);
return 0;
}
输出:
In main: create thread 0
In main: create thread 1
In main: create thread 2
In main: create thread 3
In main: create thread 4
Hello world! thread#4
Hello world! thread#3
Hello world! thread#2
Hello world! thread#1
Hello world! thread#0