简体   繁体   English

具有void *的Pthread结构参数

[英]Pthread struct argument with void*

Hy, HY,

I was wondering if something like this is possible, without using heap(malloc/calloc) Suppose I have a struct like this: 我想知道是否可以在不使用heap(malloc / calloc)的情况下实现这样的功能?假设我有一个这样的结构:

typedef struct {
  void* par1_;
  void* par2_;
}parameters;

and another one for position: 另一个职位:

typedef struct {
  short x;
  short y;
}position;

This is a function that gets called by the thread. 这是一个被线程调用的函数。

void* car(void* arg)
{
   parameters car_type = *((parameters*) arg);
   int first_par = *(int*)&car_type.par1_;
   int second_par = *(int*)&car_type.par2_;   // can I do this?
   //if yes how do I extract now values from position struct "pos.x and pos.y" 
}

From my main thread I want to mark position in the struct "position", assign that struct to the second parametar "par2_", and than send that to my function car. 我想从我的主线程中标记“ position”结构中的位置,将该结构分配给第二个参数“ par2_”,然后将其发送给我的功能车。

int main()
{
  parameters pars;
  position pos;

  pos.x = 44;
  pos.y = 25;

  pars.par1_ = (void*) CAR_TYPE; // Global Variable
  pars.par2_ = &pos;   // not sure about this?

  pthread_t tid;
  pthread_create(&tid, NULL, car, (void*) &pars);

  pthread_join(tid, NULL);

I'm sorry if this is a stupid question. 抱歉,这是一个愚蠢的问题。 Obviously I'm new to all this. 显然,我是新手。 Once again, I do not want to use heap. 再一次,我不想使用堆。 This is minimal example of my program. 这是我程序的最小示例。

I think you want something more like this; 我想你想要更多这样的东西;

void* car(void* arg)
{
   parameters car_type = *((parameters*) arg);
   int first_par = car_type.par1_; // This is CAR_TYPE is it really an int?
   position *second_par = (position *)car_type.par2_; 

   second_par->x, second_par->y;
}

Although you might just want to change your parameters struct to include the types you really want. 尽管您可能只想更改参数结构以包含您真正想要的类型。

typedef struct {
  int par1_;
  position* par2_;
}parameters;
void* car(void* arg)
{
   parameters car_type = *((parameters*) arg);
   int first_par = car_type.par1_; // This is CAR_TYPE is it really an int?
   car_type.par2_->x; //access like this
}

Not sure what you're asking, so I'll give you standard advice that seems like it pertains to the situation. 不知道您要问什么,所以我会给您标准建议,似乎与情况有关。

Lifetime 一生

Be very careful when passing pointers to stack memory. 将指针传递到堆栈存储器时要非常小心 Always keep these three things in mind: 请始终牢记以下三点:

  1. What will use the pointer? 指针将使用什么?
    What will it be used for? 它有什么用? Which functions will end up with it? 哪些功能将最终实现? You'll need to know this to deal with the next two points. 您需要知道这一点才能处理接下来的两点。
  2. Where will the pointer be stored? 指针将存储在哪里?
    If the pointer never leaves the stack, it's fine. 如果指针从不离开堆栈,那就没问题了。 If the pointer gets stored in heap memory, which has a chance of outliving the stack frame, alarm bells. 如果指针存储在堆内存中(这有可能使堆栈帧失效),则会发出警报。 If the pointer outlives the stack frame, scary unexpected data corruption is par for the course. 如果指针的寿命超过了堆栈帧,则过程中会出现可怕的意外数据损坏。 Do not allow that to happen. 不允许这种情况发生。
  3. When will the pointer be used? 什么时候使用指针?
    Anything in or called by the stack frame in which the memory is first used is OK. 第一次使用该内存的堆栈帧中的任何内容或调用它都可以。 Anything above that, and the memory is not yours to play with. 除此之外,存储器不是您的玩物。 Make sure that you never ever EVER EVER EVER return a pointer to stack memory you've just got. 请确保你永远永远永远永远返回一个指针堆栈你拿到的记忆。

To reiterate: 重申:

Do: 做:

#include <stdio.h>

int main(int argc, char *argv[]) {
    int nums = {12, 630, 43, 0};
    printf("%d", sum(nums));
}

int sum(int *num_pointer) {
    int count = 0;
    for (; *num_pointer; num_pointer += 1) {
        add(&count, *num_pointer);
    }
    return count;
}

void add(int *a, int b) {
    *a += b;
}

Don't: 别:

int main(int argc, char *argv[]) {
    print_int(get_int(7));
}

int *get_int(int value) {
    return &value;
}

void print_int(int *num) {
    printf("%d", *num);
}

Also, don't type-cast when you don't have to. 另外,在不需要时也不要键入。 It's a big sign pointing towards bad program design; 这是一个糟糕的程序设计的重要信号。 consider revising it. 考虑修改它。

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

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