C语言 将多个参数传递给 C 中的线程(pthread_create)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6524433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:02:26  来源:igfitidea点击:

Passing multiple arguments to a thread in C (pthread_create)

cmultithreadingpthreads

提问by Jary316

I am trying to pass 2 unsigned integers to a newly created thread in C (using pthread_create()) but nor an array of 2 integers or a struct seems to work.

我试图将 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);

Before calling pthread_create, dim.width and dim.height are correct. In my socket file, only width is set, height is 0, and I do not understand why.

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

Does anyone know what is wrong please and how to fix it?

有谁知道出了什么问题以及如何解决?

Thank you very much.

非常感谢。

回答by Adam Rosenfield

The way you're passing the arguments should work fine, as long as dimis not allocated on the stack. If it's on the stack, then it could become deallocated before the new thread has a chance to run, resulting in undefined behavior. If you're only creating one thread, you can use a global variable, but the better alternative is to allocate it on the heap.

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

Also, you should notbe casting the function pointer: that is undefined behavior (and in fact, it could crash due to speculative execution on the IA64 architecture). You should declare your thread procedure to return void*and avoid a function pointer cast:

此外,你应该被铸造函数指针:这是未定义的行为(事实上,它可能会崩溃,由于在IA64架构推测执行)。您应该声明您的线程过程以返回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);

回答by R.. GitHub STOP HELPING ICE

How big can width and height be? If not very big, I would do something like this:

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

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

回答by Oliver Charlesworth

You are passing a pointer to a variable with local scope. If the caller function finishes before the thread has picked up the values, then that variable will go out of scope, and its contents will be undefined.

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

回答by Nikolai Fetissov

You creating that argument structure on the stack, which is temporary storage and is re-used by the function call chain. It's probably overwritten by the time the thread starts. You need to pass a pointer to either static or heap-allocated memory.

您在堆栈上创建该参数结构,这是临时存储并由函数调用链重用。它可能在线程开始时被覆盖。您需要传递一个指向静态或堆分配内存的指针。