|
#include<pthread.h>
#include<stdio.h>
int sum;
void *runner(void *param);
int main(int argc, char *argv[])
{
pthread_tid;
pthread_attr_t attr;
if(argc!=2){
fprintf(stderr,"usage:a.out<integer value\n>");
return -1;
}
if(atoi(argv[1])<0){
fprintf(atderr,"%d must be>=0\n",atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,argv[1]);
pthread_join(tid,NULL);
printf("sum = %d\n",sum);
}
void *runner(void*param)
{
int i,upper = atoi(param);
sum = 0 ;
for(i =1;i<=upper;i++)
sum+=i;
pthread_exit(0);
}
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,argv[1]);
pthread_join(tid,NULL);
以上三个函数个别是做些什么用的?? & 形参个别代表什么??
这种的代码哪种书上才有可以推荐吗??
谢谢 |
|