|
Boost Users : |
Subject: Re: [Boost-users] [Newbie] How to create a class member thread
From: Daniele Barzotti (daniele.barzotti_at_[hidden])
Date: 2009-02-25 02:57:05
vicente.botet ha scritto:
>
>
> BTW, why do you want to start the thread on the Init function and not on the constructor?
>
Hi,
first, thanks a lot for the suggestion!
The Init function was an example. My real class is a RS232 wrapper. I
have to start a thread to continuous reading from ports, but only when
the user call the open() method.
I need the thread has a class scope because in the close() method where
I close the thread and I call thread.join()
void SerialPort::reading()
{
while (!_stoprequested) { reading.... }
}
bool SerialPort::open(void)
{
try {
_thread.reset(new boost::thread( boost::bind(&SerialPort::reading,
this));
} catch (boost::thread_resource_error e) {
// Failed to create the new thread
return false;
}
}
bool SerialPort::close(void)
{
if (m_serial_port.IsOpen()){
// If the port is open, then the reading thread is
_stoprequested = true;
try{
_thread.join();
} catch (boost::thread_interrupted e) {
//The thread is already stopped..do nothing
}
m_serial_port.Close();
}
return true;
};
So I think the pointer solution is good!
Thanks again.
Daniele.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net