我正在尝试学习 C 中的多线程编程并尝试理解基本程序。我无法理解 runner 函数以及为什么它返回一个指向 void 类型的指针并传递一个也是指向 void 的指针的参数。另外,我无法理解 main 的参数。
int sum; / this data is shared by the thread(s)
void *runner(void *param); / the thread
int main(int argc, char *argv[])
{
pthread_t tid; / the thread identifier /
pthread.attr_t attr; / set of thread attributes /
if (argc != 2) {
fprintf(stderr,"usage: a.out <integer value>\n");
return -1;
}
if (atoi(argv[1]) < 0) {
fprintf(stderr,"%d must be >= 0\n",atoi(argv[1]));
return -1;
/ get the default attributes /
pthread.attr.init (&attr) ;
/ create the thread /
pthread^create(&tid,&attr,runner,argv[1]);
/ wait for the thread to exit /
pthread_join (tid, NULL) ;
printf("sum = %d\n",sum);
/ The thread will begin control in this function /
void *runner(void *param)
{<br />
int i, upper = atoi(param);
sum = 0;<br />
for (i = 1; i <= upper; i
sum += i;
pthread_exit (0) ;