Hi, 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. What should I do in this case? I also noticed boost::thread has a default constructor but it's not copyable. What is the purpose of this default constructor? And are there any boost::thread examples? I couldn't find them in the documentation.
Olaf van der Spek wrote
I also noticed boost::thread has a default constructor but it's not copyable. What is the purpose of this default constructor?
According to the documentation the default constructor "Constructs a thread object representing the current thread of execution."
And are there any boost::thread examples? I couldn't find them in the documentation.
There are examples in the libs/thread/example folder. -delfin
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)); Or this: boost::scoped_ptrboost::thread m_thread; m_thread.reset(new boost::thread(boost::bind(&Cdns_worker::run, this));
I also noticed boost::thread has a default constructor but it's not copyable. What is the purpose of this default constructor?
Creates a boost::thread object associated with the current thread of execution.
And are there any boost::thread examples? I couldn't find them in the documentation.
C:\boost_1_33_1\libs\thread\example\*.cpp KevinH -- Kevin Heifner heifner @ ociweb.com http://heifner.blogspot.com Object Computing, Inc. (OCI) www.ociweb.com
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?
Olaf van der Spek wrote:
On 3/4/06, Kevin Heifner
wrote: 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.
I also prefer to avoid its use anywhere that I can. Of course the thread group is new'ing for you if you use it.
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?
Not that I saw. KevinH -- Kevin Heifner heifner @ ociweb.com http://heifner.blogspot.com Object Computing, Inc. (OCI) www.ociweb.com
participants (3)
-
Delfin Rojas
-
Kevin Heifner
-
Olaf van der Spek