一个简单的线程创建代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #define num 8 void *PrintHello(void *threadid){ int *id_ptr,taskid; sleep(1); id_ptr = (int *) threadid; taskid = *id_ptr; printf("hello from thread %d \n",taskid); pthread_exit(NULL); } int main(int argc,char *argv[]){ pthread_t threads[num]; int *taskids[num]; int rc,t; for(t=0;t<num;t++){ printf("Creating thread %d\n",t); taskids[t] = (int *) malloc(sizeof(int)); *taskids[t] = t; rc = pthread_create(&threads[t],NULL,PrintHello,(void*) taskids[t]); if (rc) { printf("ERROR; return code is %d\n",rc); exit(-1); } } pthread_exit(NULL); }