简体   繁体   English

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

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

Currently my program should take a list of numbers entered by the user at the command line and then find the sum of this numbers and print it out. 当前,我的程序应该获取用户在命令行中输入的数字列表,然后找到这些数字的总和并打印出来。 My code is the following, I know to store a single number entered by the user, but what if I want a list of number, separated by space? 我的代码如下,我知道要存储用户输入的单个数字,但是如果我想要一个数字列表,用空格分隔怎么办?

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

I want to be able to enter a list of numbers in the command line, and store those numbers into a list and then find the sum of all those numbers. 我希望能够在命令行中输入数字列表,并将这些数字存储到列表中,然后找到所有这些数字的总和。

, can someone tell me what is the correct way to do this? ,有人可以告诉我这样做的正确方法是什么?

It baffles me that you could write a threaded programme but did not know that: 让您感到困惑的是,您可以编写线程程序,但不知道:

you cannot cast parse an array of string to an array of int. 您不能 字符串数组解析为int数组。 you have to do the casts parsing one by one, during the sum loop. 您必须在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);
}

You also need to pass argv+1 and not argv[1] to pthread_create in main : 您还需要将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));

Some problems here: 这里有些问题:

if (argc != 2)

This means you're expecting the integer values to be quoted, ie a.out "1 2 3 4 5" . 这意味着您期望整数值被加引号,即a.out "1 2 3 4 5" If you do things this way the numbers are represented as a single string, ie argv[1] := "1 2 3 4 5" . 如果以这种方式进行操作,则数字将表示为单个字符串,即argv[1] := "1 2 3 4 5"

It's easier to check for argc < 2 and taking the arguments as a.out 1 2 3 4 5 . 检查argc < 2并将参数作为a.out 1 2 3 4 5更容易。 This way each argument gets its own string, ie argv[1] := "1", argv[2] := "2" etc. 这样,每个参数都会获得自己的字符串,即argv[1] := "1", argv[2] := "2"等。

You can of course use a quoted list instead, but then you have add some logic to extract the integers from the string (eg with strtok ) whereas argument handling can do it for you instead. 当然,您可以使用带引号的列表,但是您已经添加了一些逻辑来从字符串中提取整数(例如,使用strtok ),而参数处理可以代替您。

Second, your program expects at least six integers here, and also skips the first one (you want i to go from 0 : 其次,您的程序期望在这里至少有六个整数,并且还会跳过第一个整数(您希望i0

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

As for the upper limit, one way to convey the number of integers together with their strings is to use a struct: 至于上限,将整数与字符串一起传送的一种方法是使用结构:

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

and then use such a struct when calling pthread_create , ie 然后在调用pthread_create时使用这样的结构,即

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

and change runner accordingly: 并相应地更改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);
}

Here's the code with all changes: 这是所有更改的代码:

#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