在linux中用一个进程创建3个线程,在三个线程分别打印各自的线程id
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#define THREAD_NUM 3
void * thread_handler(void *arg);
void main()
{
int i;
pthread_t tid[THREAD_NUM];
for(i = 0; i < THREAD_NUM; i++){
pthread_create(&tid[i], NULL, thread_handler, NULL);
}
for(i = 0; i < THREAD_NUM; i++){
pthread_join(tid[i],NULL);
}
}
void * thread_handler(void *arg)
{
printf("process id: %d\tthread id: %lu\n", getpid(),pthread_self());
}