简体   繁体   English

尝试将结构实例传递给具有整数和双精度值的线程函数

[英]Trying to pass an instance of a struct to a thread function with an integer and a double value

I'm trying to pass an instance of a structure to a thread but for some reason it is printing a random value for the integer but the correct value of the double?我试图将一个结构的实例传递给一个线程,但由于某种原因,它正在为整数打印一个随机值,但为双精度打印正确的值?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//Pass an integer value and a double value to a thread (use struct!)
typedef struct param { 
    int val;
    double db;
}param_t;

void *myth(void *arg) 
{ 
    param_t myT, *myPt;
    myT = *((param_t*)arg);
    myPt = (param_t*)arg;
    
    printf("%d\n", myT.val);
    printf("%.3lf\n", myPt->db);
    pthread_exit(NULL);
}

void main() 
{ 
    pthread_t tid;
    int i = 3733;
    double d = 3733.001;
    param_t t_struct;
    param_t *p;
    p = malloc(sizeof(param_t));
    *p = t_struct;
    t_struct.val = i;
    t_struct.db = d;

    
    pthread_create(&tid, NULL, myth, (void *)&p);
    pthread_join(tid, NULL);
    return;
}

Output: 10969104 3733.001输出:10969104 3733.001

One problem is here:一个问题在这里:

*p = t_struct;
t_struct.val = i;
t_struct.db = d;

The first assignment copies the uninitialized structure t_struct .第一个赋值复制未初始化的结构t_struct Then you initialize t_struct , but that only initializes t_struct itself.然后你初始化t_struct ,但这只会初始化t_struct本身。 It doesn't modify the copy pointed to by p .不会修改p指向的副本。

Then you make it even worse by passing a pointer to the pointer.然后你通过传递一个指向指针的指针使情况变得更糟。 That means inside the thread function myth the argument arg doesn't point to a structure at all.这意味着在线程函数myth ,参数arg根本不指向结构。 Which leads to undefined behavior when you dereference the pointer.当您取消引用指针时,这会导致未定义的行为

My recommendation is to not bother with p or the dynamic allocacion at all.我的建议是根本不要理会p或动态分配。 Instead pass a pointer to the original structure t_struct :而是传递指向原始结构t_struct的指针:

pthread_create(&tid, NULL, myth, & t_struct);

This makes no sense:这没有任何意义:

param_t t_struct;
param_t *p;
p = malloc(sizeof(param_t));
*p = t_struct;       // t_struct isn't initialized, so this is undefined behaviour.
t_struct.val = i;    // Has no effect on `p` or `*p`.
t_struct.db = d;     // Has no effect on `p` or `*p`.

Maybe you were going for也许你是为了

param_t t_struct;
param_t *p;
p = malloc(sizeof(param_t));
t_struct.val = i;
t_struct.db = d;
*p = t_struct;

The following would be better:下面的会更好:

param_t t_struct;
t_struct.val = i;
t_struct.db = d;

param_t *p = &t_struct;

You can also use the following:您还可以使用以下内容:

param_t *p = malloc(sizeof(param_t));
p->val = i;
p->db = d;

Unlike the previous solution, this last solution doesn't require t_struct to keep existing until the thread is joined与之前的解决方案不同,这最后一个解决方案不需要t_struct在加入线程之前保持存在


There's a second problem.还有第二个问题。

arg is really a param_t ** , but you treat it as a param_t * . arg实际上是param_t ** ,但您将其视为param_t *

Fixed:固定的:

void *myth(void *arg)   // arg is really a param_t *
{ 
    param_t *p = (param_t*)arg;;
    printf("%d\n", p->val);
    printf("%.3lf\n", p->db);
    pthread_exit(NULL);
}

pthread_create(&tid, NULL, myth, p);

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

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