线程创建

一个简单的线程创建代码

#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);

}
View Code

 

posted @ 2017-02-15 11:31  Erinlp  阅读(59)  评论(0编辑  收藏  举报