Suggestion about implementing events

B

Barzo

Hi,

I need to create a class to read/write from a Serial port and I need a
suggestion about implementing events.
I've implemented the observer pattern with an interface more or less
like this:

class ISerialDevEvents
{
virtual void DataArrived(void) = 0;
}

class SerialDev
{
public:
SerialDev(ISerialDevEvents *handler);
int Open()
{
/* Create thread and Run it ( Reading() ) */
}

protected:
ISerialDevEvents _handler;

private:
Reading()
{
do while (!StopRequest)
{
....
if (eventHandler != 0)
_handler->DataArrived(void);
}
}
};

class User : public CThreadEventHandler
{
main()
{
SerialDev Dev;
Dev.Open(this);
}

virtual void DataArrived()
{
Dev.GetData();
}
};

But with this method a user class has to implements all virtual
methods of its interface also if it doesn't use it.
What about if, instead of an interface, I made the open() method to
accept a pointer to member function?

template<class R, class T>
Open( R (T::*pmf)() )

If the classes lives in separated threads is it legal?

PS [OT]: Someone knows if the Boost::serial_port is able to manage PIN
like DTR, RTS etc..?

Tnx,
Daniele.
 
N

Noah Roberts

Barzo said:
Hi,

I need to create a class to read/write from a Serial port and I need a
suggestion about implementing events.

Check out boost::signal.
 
B

Barzo

Check out boost::signal.

Hi,

I've gave a look to Signals and they are very intresting, but in their
FAQ I found this:

Is Boost.Signals thread-safe?
No. Using Boost.Signals in a multithreaded concept is very dangerous,
and it is very likely that the results will be less than satisfying.
Boost.Signals will support thread safety in the future.

So, the class SerialDev cannot "send" a signal like 'void Sig(void)'
to a slot that lives in another thread?

thanks!
Daniele.
 
E

Eric.Malenfant

I've gave a look to Signals and they are very intresting, but in their
FAQ I found this:

Is Boost.Signals thread-safe?
No. Using Boost.Signals in a multithreaded concept is very dangerous,
and it is very likely that the results will be less than satisfying.
Boost.Signals will support thread safety in the future.

Signals2, initially named "thread safe signals" (http://www.comedi.org/
projects/signals2/libs/signals2/doc/html/) was recently accepted into
boost: http://lists.boost.org/boost-announce/2008/11/0211.php
It is not yet in the Boost official distribution, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top