简体   繁体   中英

How do you pass in structs in multiple threads using C++?

Recently, I have been creating a program that creates multiple threads using structs. In my subroutine, I've noticed that my values in my struct are never passed (they're random things). I've been told to instantiate a new struct with every thread created, but this doesn't work for me (probably because of syntax).

I'm looking for a way to make a small change so that my values from the struct are passed into the subroutine when the threads are being created.

Struct:

struct Node {
    long int upper_bound;
    long int lower_bound;
    int sum = 0;
};

In main:

struct Node *node;

Creating threads:

node -> upper_bound = interval;
node -> lower_bound = min;
for( int i = 0; i < num_threads; i++ ) {
            ids[i] = i;
            cout << "Making a thread with these boundaries: " << node -> lower_bound << " " << node -> upper_bound << endl;
            rc = pthread_create(&thrdid[i],NULL,sub,(void *) &node);
            node -> lower_bound += (interval+1);
            node -> upper_bound += interval;
            //make a new thread, but where?
}

In subroutine:

void* sub(void *arg) {

    int i;
    i = *( (int *)arg );

    Node* aNode = static_cast<Node*>(arg);
    ......
}

What am I doing wrong? Why aren't my values being passed in?

You will have to create instances of Node for each threads.

It can be done like this:

node = new Node; // create an instance of Node

node -> upper_bound = interval;
node -> lower_bound = min;
for( int i = 0; i < num_threads; i++ ) {
            ids[i] = i;
            cout << "Making a thread with these boundaries: " << node -> lower_bound << " " << node -> upper_bound << endl;
            rc = pthread_create(&thrdid[i],NULL,sub,(void *) node); // pass pointers to Node instead of pointers to pointers
            struct Node *next_node = new Node; // create next instance of Node
            next_node -> lower_bound = node -> lower_bound + (interval+1);
            next_node -> upper_bound = node -> upper_bound + interval;
            node = next_node;
}

Another way is allocating Node for all threads at once:

std::vector<Node> nodes(num_threads);

node[0].upper_bound = interval;
node[0].lower_bound = min;
for( int i = 0; i < num_threads; i++ ) {
            ids[i] = i;
            cout << "Making a thread with these boundaries: " << node -> lower_bound << " " << node -> upper_bound << endl;
            rc = pthread_create(&thrdid[i],NULL,sub,(void *) &nodes[i]);
            if (i + 1 < num_threads) {
                        nodes[i + 1].lower_bound = nodes[i].lower_bound + (interval+1);
                        nodes[i + 1].upper_bound = nodes[i].upper_bound + interval;
            }
}

You previously declared node as a pointer

struct Node *node;

and then took it's addres in pthread_create:

rc = pthread_create(&thrdid[i],NULL,sub,(void *) &node);

Which resulted in passing pointer to pointer. Instead just use:

rc = pthread_create(&thrdid[i],NULL,sub,(void *) node);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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