线程创建

来源:互联网 发布:保险网络增员话术 编辑:程序博客网 时间:2024/05/01 04:56
一个简单的线程创建代码

#include <pthread.h>#include <stdio.h>#include <stdlib.h>#define num 8void *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);}
View Code

 

0 0
原创粉丝点击