Problem with boost:thread
Hey, I'm trying to start a thread from a Class but the only way I did it was
using a pointer.
The thing is that when I assign the thread to the pointer it wont start
unless I use join().
I need it to star right away is created just like when I create a thread
(boost::thread myThread(funcName);)
I was trying to use thread_group to do this, but create_thread is not
working.
*Here's an example of what I was doing:*
*(Here I used the pointer)*
class Thread {
public:
Thread(){};
virtual ~Thread(){};
virtual void run() = 0;
void start(){m_Thread = new boost::thread( &Thread::run, this ); };
//wont start unless I add m_Thread.join();
private:
boost::thread* m_Thread;
};
*and now... I am trying to use thread_group:*
class Thread {
public:
Thread(){};
virtual ~Thread(){};
virtual void run() = 0;
void start(){g_Thread.create_thread(&Thread::run); };
private:
boost::thread_group g_Thread;
};
*But I receive an error:*
/usr/local/include/boost/thread/detail/thread.hpp: In member function 'void
boost::detail::thread_data<F>::run() [with F = void (Thread::*)()]':
main.cpp:61: instantiated from here
/usr/local/include/boost/thread/detail/thread.hpp:56: error: must use '.*'
or '->*' to call pointer-to-member function in
'((boost::detail::thread_data
void start(){m_Thread = new boost::thread( &Thread::run, this ); }; //wont start unless I add m_Thread.join();
The thread is created and started anyway. Join() just tells the *caller* thread to wait until m_Thread finishes.
void start(){g_Thread.create_thread(&Thread::run); };
g_Thread.create_thread(boost::bind(&Thread::run, this));
Thanks!
But I said that the thread wont start because in other class I defined, that
inherits from this "Thread" class.
I implemented the abstract run() method so it just prints a line to the
output:
class Trash : public Thread {
std::string garbage;
public :
Trash(std::string newString){garbage = newString;};
void run(){ std::cout << "RUNNING" << std::endl;};
}
int main(int argc, char** argv) {
Trash myTrash("hello");
myTrash.start();
}
and whenever I run the project (using boost::thread or using
boost::thread_group with the correction you provided) it wont show the
output... "RUNNING".
BUT, if I add m_Thread.join(); it shows that output.
Any idea why?
Thanks!!
On Thu, Mar 4, 2010 at 1:02 PM, Igor R
void start(){m_Thread = new boost::thread( &Thread::run, this ); }; //wont start unless I add m_Thread.join();
The thread is created and started anyway. Join() just tells the *caller* thread to wait until m_Thread finishes.
void start(){g_Thread.create_thread(&Thread::run); };
g_Thread.create_thread(boost::bind(&Thread::run, this)); _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
True.
I added a useless cin just to see what happens and as the program is still
"alive" I get to see the thread.
Do you know how can I avoid the main thread to end before my thread is
executed?
Thanks!!
Dann
On Thu, Mar 4, 2010 at 1:17 PM, Igor R
Trash myTrash("hello"); myTrash.start();
At this point your main thread *ends* and the program ends, so the 2nd thread doesn't have an opportunity to do anything. That's why you need to wait for the 2nd thread -- by means of join(). _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thank you VERY much!!
Everything's clear now
Dann
On Thu, Mar 4, 2010 at 1:33 PM, Igor R
Do you know how can I avoid the main thread to end before my thread is executed?
Use join(). _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Daniel Veneros
-
Eric J. Holtman
-
Igor R
-
strasser@uni-bremen.de