Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[kinda solved] c++ boost::thread question
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Vojko
n00b
n00b


Joined: 11 Aug 2006
Posts: 27

PostPosted: Sat Oct 13, 2007 3:05 pm    Post subject: [kinda solved] c++ boost::thread question Reply with quote

Ok i have the following code and i would like to make it work:

Code:
#include <iostream>
#include <boost/thread/thread.hpp>

using namespace std;

class TestThread : public boost::thread
{
    int counter;
    string _text;

    public:
        TestThread(string text, int start = 1)
        {
            counter = start;
            _text = text;           
        }

        void execute()
        {
            while (counter <= 20)
            {
                cout << _text + " " << counter++ << endl;
            }
        }

        void run()
        {       
            thread(&execute);
            join();
        }
};

int main(int argc, char* argv[])
{
    TestThread *tt = new TestThread("Thread1");
    tt->run();

    return 0;

}


i get the following error while compiling with
Code:
g++ -lboost_thread threadclass.cpp

Code:
threadclass.cpp: In member function 'void TestThread::run()':
threadclass.cpp:28: error: 'execute' declared as reference but not initialized


so, is it possible to make a thread class like this with boost::thread or am i stuck with the official method which is not very object oriented as described here http://www.ddj.com/cpp/184401518?pgno=2

Thanks for your help.


Last edited by Vojko on Sun Oct 14, 2007 5:09 am; edited 1 time in total
Back to top
View user's profile Send private message
dentharg
Guru
Guru


Joined: 10 Aug 2004
Posts: 438
Location: /poland/wroclaw

PostPosted: Sat Oct 13, 2007 4:28 pm    Post subject: Reply with quote

First: execute() is a *member* method not a global function as hello() is in example link you gave.
So, I would assume that the pointer to it is &TestThread::execute.
However it might not be possible to give a pointer to a non-static function.

I would do like this: make execute a static function that takes a pointer to TestThread as only parameter
and operates on this pointer.
Back to top
View user's profile Send private message
Vojko
n00b
n00b


Joined: 11 Aug 2006
Posts: 27

PostPosted: Sun Oct 14, 2007 5:08 am    Post subject: Reply with quote

The main problem was that you couldn't (well at least i didn't know how) pass any arguments to that worker function that initializes the thread. I found out they have that covered too. What i'll do is that i'll have a class that will do something and a worker function worker(string param1) that'll call it. I'll make that function a thread with
Code:
boost::thread thrd(boost::bind(&worker, "some text"));
thrd.join();
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum