std::thread question

Get answers to all your basic programming questions. No Ogre questions, please!
GavinL
Gnoblar
Posts: 19
Joined: Fri Sep 09, 2011 11:32 pm

std::thread question

Post by GavinL »

Hello,

I've been trying to use the std::thread library (in gcc) to multithread some parts of my code. I've used threads before, but I'm not sure how to do this using std::thread. I have a class that has a few methods, each method needs to be able to run on a thread and have access to the classes' member variables (threads do not access the same member variables, so no need for mutexes, yet).

Now I can run the member methods in the thread, but i cannot get access to the member variables, everytime I do, the program crashes. Here is a quick test bit of code of what I essentially need to do.

So I have a member variable, int test, that my thread needs access too. But if I try to access the variable, the program crashes. I tried passing a pointer to the object to the thread function so I could access the variable by pointer->test, but that didn't work either. Anyone know of a way I can tackle this?

Code: Select all

#include <stdio.h>
#include <thread>
#include <time.h>
#include <unistd.h>
#include <iostream>

class background
{
    public:

        int                     test;
        //VeryLargeDataStructure  LargeData;

        background()
        {
            test = 0;
        };

        void changeTest(int newt)
        {
            test = newt;
        }


        void doWork(int loop)
        {
            // perform some heavy computation on test and LargeData


            std::cout <<"Thread started\n";
            for(int i=0;i<loop;i++)
            {
                std::cout << "Doing work: " << loop << "\n";
                for(int j=0;j<loop;j++)
                {
                    if( i % j == 0 )
                    {
                        // This line causes problems.
                        test++;
                    }
                }
                usleep(10);
            }

        };

};


int main()
{
    background * g = new background();

    // How do i run g->doWork(5000) in a background thread, and also have access to g's member variables?
    std::thread t(&background::doWork, g , 5000);

    t.join();

    std::cout << "waiting\n";
    return 0;
};

CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58

Re: std::thread question

Post by CABAListic »

I gave it a quick go, and there is an error in your code, but the debugger points me to one line above the one you think is the problem: The if clause i % j == 0 is invalid if j == 0. So you either need to start j at 1 or rewrite your if clause:

Code: Select all

if ( j == 0 || i % j == 0 )
Aside from that, the sample runs fine for me.
GavinL
Gnoblar
Posts: 19
Joined: Fri Sep 09, 2011 11:32 pm

Re: std::thread question

Post by GavinL »

Hmm you're right. I didn't realize. Because if I comment out the test++ line; I guess the compiler's optimization would remove the entire if statement since it doesn't do anything, which would allow the code the run fine.

I was running the program in the QT Creator Console. I ran it in the linux terminal and it showed me a floating point exception. Stupid QTCreator..

Thanks for your help!