繁体   English   中英

如何存储输入列表以进行多线程编程

[英]How to store a list of inputs for multi-thread programming

当前,我的程序应该获取用户在命令行中输入的数字列表,然后找到这些数字的总和并打印出来。 我的代码如下,我知道要存储用户输入的单个数字,但是如果我想要一个数字列表,用空格分隔怎么办?

#include <pthread.h>
#include <stdio.h>
int sum; /* this data is shared by the thread(s) */
void *runner(char **); /* threads call this function */
int main(int argc, char *argv[])
{
pthread_t tid; /* the thread identifier */
pthread_t tid2;

pthread_attr_t attr; /* set of thread attributes */

if (argc != 2) {
fprintf(stderr,"usage: a.out <integer values>\n");
return -1;
}

pthread_attr_init(&attr);

pthread_create(&tid,&attr,(void(*)(void *))(runner),(void *)(argv+1));

pthread_join(tid,NULL);

printf("sum = %d\n",sum);
}
/* The thread will begin control in this function */
void *runner(char **param)
{
int i;
sum = 0;
for (i = 1; i <= 5; i++)
   sum = sum + atoi(param[i]);

pthread_exit(0);
}

我希望能够在命令行中输入数字列表,并将这些数字存储到列表中,然后找到所有这些数字的总和。

,有人可以告诉我这样做的正确方法是什么?

让您感到困惑的是,您可以编写线程程序,但不知道:

您不能 字符串数组解析为int数组。 您必须在sum循环中进行一次 强制转换的转换

/* The thread will begin control in this function */
void *runner(char **param)
{
int i;

sum = 0;
for (i = 1; i <= upper; i++)
    sum = sum + atoi(param[i]);
pthread_exit(0);
}

您还需要将argv+1而不是argv[1]传递给main pthread_create

// the runner function declaration
void *runner(char **);

// the thread creation
pthread_create(&tid,&attr,(void *(*)(void *))(runner),(void *)(argv+1));

这里有些问题:

if (argc != 2)

这意味着您期望整数值被加引号,即a.out "1 2 3 4 5" 如果以这种方式进行操作,则数字将表示为单个字符串,即argv[1] := "1 2 3 4 5"

检查argc < 2并将参数作为a.out 1 2 3 4 5更容易。 这样,每个参数都会获得自己的字符串,即argv[1] := "1", argv[2] := "2"等。

当然,您可以使用带引号的列表,但是您已经添加了一些逻辑来从字符串中提取整数(例如,使用strtok ),而参数处理可以代替您。

其次,您的程序期望在这里至少有六个整数,并且还会跳过第一个整数(您希望i0

for (i = 1; i <= 5; i++)
   sum = sum + atoi(param[i]);

至于上限,将整数与字符串一起传送的一种方法是使用结构:

struct arg_struct {
    int argc;
    char **argv;
};

然后在调用pthread_create时使用这样的结构,即

struct arg_struct args = { argc-1, argv+1 };
pthread_create(&tid,&attr,(void(*)(void *))(runner),(void *)(&args));

并相应地更改runner

void *runner(struct arg_struct *param)
{
int i;
sum = 0;
for (i = 0; i < param->argc; i++)
   sum = sum + atoi(param->argv[i]);

pthread_exit(0);
}

这是所有更改的代码:

#include <pthread.h>
#include <stdio.h>

struct arg_struct {
    int argc;
    char **argv;
};

int sum; /* this data is shared by the thread(s) */
void *runner(struct arg_struct *); /* threads call this function */
int main(int argc, char *argv[])
{
pthread_t tid; /* the thread identifier */
pthread_t tid2;

pthread_attr_t attr; /* set of thread attributes */

struct arg_struct args = { argc-1, argv+1 };

if (argc < 2) {
fprintf(stderr,"usage: a.out <integer values>\n");
return -1;
}

pthread_attr_init(&attr);

pthread_create(&tid,&attr,(void *(*)(void *))(runner),(void *)(&args));

pthread_join(tid,NULL);

printf("sum = %d\n",sum);
}
/* The thread will begin control in this function */
void *runner(struct arg_struct *param)
{
int i;
sum = 0;
for (i = 0; i < param->argc; i++)
   sum = sum + atoi(param->argv[i]);

pthread_exit(0);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM