【问题标题】:Passing multiple arguments to a thread in C (pthread_create)将多个参数传递给 C 中的线程(pthread_create)
【发布时间】:2011-06-29 16:57:27
【问题描述】:

我正在尝试将 2 个无符号整数传递给 C 中新创建的线程(使用 pthread_create()),但 2 个整数的数组或结构似乎也不起作用。

// In my socket file

struct dimension {
    unsigned int width;
    unsigned int height;
};

unsigned int width, height;

void setUpSocket(void* dimension) {

    struct dimension* dim = (struct dimension*) dimension;

    width = dim->width;
    height = dim->height;

    printf("\n\nWidth: %d, Height: %d\n\n", width, height);

}

// In main.cpp

// Pass a struct in pthread_create
struct dimension dim;
dim.width = w;
dim.height = h;

pthread_create(&ph, &attr, (void * (*)(void *)) setUpSocket, (void *) &dim);

在调用 pthread_create 之前,dim.width 和 dim.height 是正确的。在我的socket文件中,只设置了宽度,高度为0,不明白为什么。

请问有谁知道问题出在哪里以及如何解决?

非常感谢。

【问题讨论】:

  • 你的线程应该返回void *而不是void。所以你可以停止做我不明白的那种时髦的类型转换:P

标签: c multithreading pthreads


【解决方案1】:

您传递参数的方式应该可以正常工作,只要dim 未在堆栈上分配。如果它在堆栈上,那么它可能会在新线程有机会运行之前被释放,从而导致未定义的行为。如果您只创建一个线程,则可以使用全局变量,但更好的选择是在堆上分配它。

另外,您应该强制转换函数指针:这是未定义的行为(实际上是it could crash due to speculative execution on the IA64 architecture)。您应该声明您的线程过程以返回 void* 并避免函数指针转换:

void *setUpSocket(void* dimension) {

    struct dimension* dim = (struct dimension*) dimension;

    width = dim->width;
    height = dim->height;
    // Don't leak the memory
    free(dim);

    printf("\n\nWidth: %d, Height: %d\n\n", width, height);

    return 0;
}

// In main.cpp

// Pass a struct in pthread_create (NOT on the stack)
struct dimension *dim = malloc(sizeof(struct dimension));
dim->width = w;
dim->height = h;

pthread_create(&ph, &attr, setUpSocket, dim);

【讨论】:

    【解决方案2】:

    宽度和高度可以有多大?如果不是很大,我会这样做:

     pthread_create(&ph, &attr, setUpSocket, (void *)(65536*height+width));
    

    【讨论】:

      【解决方案3】:

      您正在传递一个指向具有本地范围的变量的指针。如果调用函数在线程获取值之前完成,则该变量将超出范围,其内容将未定义。

      【讨论】:

      • 非常感谢您的帮助,您是对的,现在已修复,谢谢!
      【解决方案4】:

      您在堆栈上创建该参数结构,该结构是临时存储并由函数调用链重复使用。它可能在线程启动时被覆盖。您需要将指针传递给静态或堆分配的内存。

      【讨论】:

      • 非常感谢,你说得对,现在已经修复了,非常感谢!
      猜你喜欢
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2016-10-05
      • 1970-01-01
      • 2023-03-31
      • 2016-01-07
      • 2012-06-25
      相关资源
      最近更新 更多