简体   繁体   English

POSIX线程,将多个参数传递给具有结构的函数

[英]POSIX thread, passing multiple arguments to function with a struct

So I've spent the last few hours trying to google and figure out what is wrong with my code but I couldn't figure out. 因此,我花了最后几个小时尝试使用google找出我的代码出了什么问题,但我无法弄清楚。

I'm a student and I just started learning about threads etc so this is all brand new to me and I'm not very experienced. 我是一名学生,刚开始学习线程等知识,所以这对我来说是全新的,我也不是很有经验。

Answers on google (and on here) were usually answers to one particular problem with the code and I couldn't figure out how to actually get this thing to work. 谷歌(和这里)的答案通常是代码中一个特定问题的答案,我不知道该如何真正地工作。

Here's a very simplified version of my code: 这是我的代码的非常简化的版本:

http://pastebin.com/wst8Yw8z http://pastebin.com/wst8Yw8z

#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

using namespace std;

struct Data{
    string a;
    string b;
};

void* thread_func( void *param ){

    struct Data *input = (struct Data*)param;

    string data1 = input->a;
    string data2 = input->b;

    cout << "You said: " << data1 << " " << data2 << endl;

    return NULL;
}

int main( int argc, char *argv[] )
{
    pthread_t child;
    string arg, arg2;

    struct Data *input;

    cout << "Input 1: " << endl;
    cin >> arg;
    cout << "Input 2: " << endl;
    cin >> arg2;

    input->a = arg;
    input->b = arg2;

    pthread_create( &child, NULL, thread_func, (void *)&input);

    pthread_join( child, NULL );
    cout << "Synced" << endl;
    return 0;
}

So I have a struct Data that I want to use to pass multiple arguments to the function thread_func. 所以我有一个结构数据,我想用来将多个参数传递给函数thread_func。

My code actually compiles (at least on linux) but when I input both values, I get Segmentation fault. 我的代码实际上已经编译(至少在Linux上),但是当我输入两个值时,都会遇到细分错误。

I am clearly doing something wrong and my guess would be the line 18, however I am not experienced enough to figure this out on my own and I'm asking you guys for help. 我显然做错了,我的猜测可能是18号线,但是我没有足够的经验来独自解决这个问题,因此我想向大家寻求帮助。

What am I doing wrong with this struct to pass multiple arguments to the function? 我将这个结构的多个参数传递给函数时,我在做什么呢?

My actual assignment is a bit more complicated than this but I did my best to make it as clear as possible. 我的实际任务要比这复杂一些,但是我尽力使它尽可能清晰。

In your main() function, this: 在您的main()函数中,这是:

struct Data *input;

creates a pointer to a struct Data , but doesn't create an actual struct Data object itself. 创建指向struct Data的指针,但不创建实际的struct Data对象本身。 You need to use here: 您需要在这里使用:

struct Data input;

and then: 接着:

input.a = arg;
input.b = arg2;

and the rest should work fine. 其余应正常工作。

This line: 这行:

struct Data *input;

Defines input as a pointer, but it never allocates the object before later using it to store your strings here: 将输入定义为指针,但是在以后使用它在此处存储字符串之前,它永远不会分配对象:

input->a = arg;
input->b = arg2;

Based on your call to pthread_create I suspect you do not want input to be a pointer at all. 根据您对pthread_create的调用,我怀疑您根本不希望input成为指针。 Remove the * , and change the assignments to: 删除* ,然后将分配更改为:

input.a = arg;
input.b = arg2;

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

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