On 3/4/06, Kevin Heifner
Olaf van der Spek wrote:
I'm converting code using pthreads to boost::thread. I've now got this code: Cdns_worker::Cdns_worker(): m_run(true), m_thread(boost::bind(&Cdns_worker::run, this)) { // pthread_cond_init(&m_condition, NULL); // pthread_mutex_init(&m_mutex, NULL); // pthread_create(&m_thread, NULL, Cdns_worker::run, this); }
The data the thread is using is in Cdns_worker, but it seems impossible to delay the thread creation until the end of the constructor, like I could with pthreads.
Not sure why you need to delay thread creation until the end of the constructor. Why not just make sure that m_thread is the last attribute in your header so that it is created last?
What should I do in this case?
You could do this:
m_thread_group.create_thread(boost::bind(&Cdns_worker::run, this));
A thread group is indeed a good idea, that way I can also choose to use more threads.
Or this:
boost::scoped_ptrboost::thread m_thread; m_thread.reset(new boost::thread(boost::bind(&Cdns_worker::run, this));
But that requires me to use new.
And are there any boost::thread examples? I couldn't find them in the documentation.
C:\boost_1_33_1\libs\thread\example\*.cpp
I searched at http://boost.org/doc/html/threads.html and don't see examples mentioned. Are they not linked in the online documentation?