首页 > 编程语言 > 详细

线程创建

时间:2017-02-15 12:15:55      阅读:203      评论:0      收藏:0      [点我收藏+]

一个简单的线程创建代码

技术分享
#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

 

线程创建

原文:http://www.cnblogs.com/qianboping/p/6400696.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!