Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Passing arguments to sf::Thread  (Read 4082 times)

0 Members and 2 Guests are viewing this topic.

qnix

  • Newbie
  • *
  • Posts: 1
    • View Profile
Passing arguments to sf::Thread
« on: December 30, 2013, 05:17:48 am »
I need some help passing an argument to a thread. I'm trying to update a map of snow, and I need to pass a class containing the section of the map that the thread needs to process to the thread. This is all done inside an object. I can't for the life of me figure it out. As it stands, my code returns a [Called object type 'void (Map::*)(Vector4i)' is not a function or function pointer] error upon build.

Any help would be appreciated.



The class containing the data:
class Vector4i {
public:
    int x,y,z,a;
    Vector4i() {}
    Vector4i(int x_, int y_, int z_, int a_) {
        x = x_;
        y = y_;
        z = z_;
        a = a_;
    }
};

The snow updator functions:
void tickSnowHelper(Vector4i vec) { //launched in thread (hopefully)
        for (int y = vec.x; y < vec.y; y++) {
            for (int x = vec.z; x < vec.a; x++) {
                if (read(x, y) == 1 /* || read(x, y) == 2 */) {
                    if (read(x, y + 1) == 0) {
                        write(x, y, 0);
                        write(x, y + 1, -2);
                    } else if (read(x + 1, y + 1) == 0 || read(x - 1, y + 1) == 0) {
                        if (read(x - 1, y + 1) == 0 && read(x + 1, y + 1) == 0) {
                            if (rand()%2) {
                                write(x, y, 0);
                                write(x + 1, y + 1, -2);
                            } else {
                                write(x, y, 0);
                                write(x + 1, y + 1, -2);
                            }
                        } else if (read(x - 1, y + 1)) {
                            write(x, y, 0);
                            write(x + 1, y + 1, -2);
                        } else if (read(x + 1, y + 1)) {
                            write(x, y, 0);
                            write(x + 1, y + 1, -2);
                        }
                    }
                }
            }
        }
    }
    void tickSnow() {
        Vector4i sector1(0,MAP_HEIGHT/2,0,MAP_WIDTH/2);
        // x-
        // --
        Vector4i sector2(0,MAP_HEIGHT/2,MAP_WIDTH/2,MAP_WIDTH);
        // -x
        // --
        Vector4i sector3(MAP_HEIGHT/2,MAP_HEIGHT,0,MAP_WIDTH/2);
        // --
        // x-
        Vector4i sector4(MAP_HEIGHT/2,MAP_HEIGHT,MAP_WIDTH/2,MAP_WIDTH);
        // --
        // -x
       
        sf::Thread helper1(&Map::tickSnowHelper,this);
        sf::Thread helper2(&Map::tickSnowHelper,this);
        sf::Thread helper3(&Map::tickSnowHelper,this);
        sf::Thread helper4(&Map::tickSnowHelper,this);
        helper1.launch();
        helper2.launch();
        helper3.launch();
        helper4.launch();
       
        // finalizes snow map
        for (int y = 0; y < MAP_HEIGHT; y++) {
            for (int x = 0; x < MAP_WIDTH; x++) {
                if (read(x, y) == -2) {
                    write(x, y, 1);
                }
                if (read(x, y) == -3) {
                    write(x, y, 2);
                }
            }
        }
    }

Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Passing arguments to sf::Thread
« Reply #1 on: December 30, 2013, 05:36:07 am »
sf::Thread does not take methods that have parameters. That's what "Called object type 'void (Map::*)(Vector4i)' is not a function or function pointer" means -- it only works with functions or functors with a single argument.

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Passing arguments to sf::Thread
« Reply #2 on: December 30, 2013, 11:42:31 am »
Even if you fix the compiler error by a cast that code is wrong. There are overlaps between the areas and there is no synchronization and the threads would run while the other finalize-loop is running.
You better avoid using threads if you dont know how to deal with this.

Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Passing arguments to sf::Thread
« Reply #3 on: December 31, 2013, 09:03:51 pm »
For future reference, I think the solutions here (http://stackoverflow.com/questions/12338695/c11-styled-callbacks) might help you later down the road to making a styled method callback. I haven't tried it yet, but it might give you some ideas.