View previous topic :: View next topic |
Author |
Message |
Vojko n00b
Joined: 11 Aug 2006 Posts: 27
|
Posted: Sat Oct 13, 2007 3:05 pm Post subject: [kinda solved] c++ boost::thread question |
|
|
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 |
|
|
dentharg Guru
Joined: 10 Aug 2004 Posts: 438 Location: /poland/wroclaw
|
Posted: Sat Oct 13, 2007 4:28 pm Post subject: |
|
|
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 |
|
|
Vojko n00b
Joined: 11 Aug 2006 Posts: 27
|
Posted: Sun Oct 14, 2007 5:08 am Post subject: |
|
|
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 |
|
|
|