简体   繁体   English

将结构作为值参数传递给线程

[英]Passing a struct to a thread as a value parameter

I want to create more than one thread with a vector of threads and I'm trying to pass a struct with values that are changing with every call. 我想用线程向量创建多个线程,并且我试图传递一个结构,其中的值随每次调用而变化。 The problem is I don't want to pass the struct as a memory location with &, because every thread receives a different value of the struct. 问题是我不想将结构作为带有&的内存位置传递,因为每个线程都接收到不同的结构值。 I want to pass the struct as a value parameter. 我想将结构作为值参数传递。 When I try, the gcc compiler throws an error: Violation segment ( `core 'generated). 当我尝试时,gcc编译器抛出一个错误:违规段(生成“核心”)。 Here is part of my code: 这是我的代码的一部分:

//the struct
struct intervalos{
  int from;
  int to;
  int number;
};


// the calls
for (int i = 0; i<=num ; i++){
    struct intervalos *inter;

    /*re asign the values of the struct...*/

    pthread_create( &vector[i], NULL, fun, inter);
}


//function of threads
void* fun(void* data) {
    struct intervalos *inter = data ;
    /* do something else */
}

You could allocate the data on the heap one of the functions in the malloc family. 您可以在堆上的数据中分配malloc系列中的一个函数。 The caller of pthread_create would not free the allocated memory, instead the new thread would do it, after it is done with processing the data (which could be after initial processing or just before thread exit, depending on what the thread does). pthread_create的调用者不会释放分配的内存,而是新的线程会在完成处理数据之后(可能在初始处理之后或在线程退出之前,取决于线程的作用)。

The fact that you can pass heap pointers between threads in this way makes malloc considerably more complicated, so you might as well use this functionality. 事实上,您可以以这种方式在线程之间传递堆指针,这使得malloc变得更加复杂,因此您也可以使用此功能。

You are not initializing your inter variable, thus causing the program to throw a segfault. 您没有初始化您的inter变量,从而导致程序抛出段错误。 Normally, you will have to use calls like malloc() to allocate memory for it before passing the pointer to any other function. 通常,在将指针传递给任何其他函数之前, 必须使用类似malloc()调用为其分配内存。

Alternatively, if you insist that you pass an unallocated pointer to the pthread_create function, you can pass along a reference to the pointer, ie pthread_create(&vector[i], NULL, fun, &inter) , and then use *inter = malloc(sizeof(struct intervalos)) to allocate memory for it within each thread. 或者,如果您坚持将未分配的指针传递给pthread_create函数,则可以传递对指针的引用,即pthread_create(&vector[i], NULL, fun, &inter) ,然后使用*inter = malloc(sizeof(struct intervalos))在每个线程内为它分配内存。

You can only pass a pointer to a pthread_t , so you cannot pass data by value, only by reference. 您只能将指针传递给pthread_t ,因此您不能仅通过引用按值传递数据。

struct intervalos * inter = malloc(sizeof(intervalos));
if (!inner) {
    // Handle out of memory error
}
inter->from = ...;
inter->to = ...;
inter->number = ...;

int error = pthread_create(&vector[i], NULL, fun, inter);
if (error) {
    // Thread was not created.
    // Free allocation to not leak memory.
    free(inner);
    // Handle cannot create thread error
}

And in the thread: 并在线程中:

void * fun ( void * data ) {
    struct intervalos * inter = data ;

    // ... Do whatever you want ...

    // Once done, free allocation or you'll leak memory
    free(inter);
    return NULL;
}

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

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