1. 环境
ubuntu 16.04
2. 代码
pthread.c(一次创建两 pthreads 线程)
#include
#include
#include
#include
void pthreadsPrinter(void){
printf("Print it.\n \n");
pthread_exit(0);
}
int main(){
int i;
i = 0;
pthread_t pid;
while(1){
i++;
// new pthreads 1/2
pthread_create(&pid, NULL, (void *)pthreadsPrinter, (void *)&i);
printf("time: %d; pid: %lu\n",i,pid);
// new pthreads 2/2
pthread_create(&pid, NULL, (void *)pthreadsPrinter, (void *)&i);
printf("time: %d; pid: %lu\n",i,pid);
sleep(1);
}
}
3. 运行
gcc pthread.c -o pthread -lpthread
./pthread
参考
http://blog.csdn.net/ithomer/article/details/5921003
http://blog.csdn.net/maopig/article/details/52261883