Re: Thread & Serial port
- From: Paul Davis <pjdavis engineering uiowa edu>
- To: Christophe ASSOUS <cube6_2000 yahoo fr>, gtk-list gnome org
- Cc:
- Subject: Re: Thread & Serial port
- Date: Tue, 23 May 2006 05:32:07 -0500
Christophe ASSOUS wrote:
Hello, I would want to create three thread, each reading and writing
on a serial port at the same time. Is it possible ? Would you have an
idea on how doing the management of the serial port by the thread?
If you have already done it, I would be pleased you to send me
information or source code about this topic. (cube6_2000 yahoo fr)
Cordially, Kris
I'm actually getting ready to write something similar, but for sockets.
My plan is to create an object to wrap around the file descriptor that I
can synchronize for reading and writing. Then each thread can read and
write to their hearts desire.
I use pthread_mutex_t for locking. There are also some Glib classes for
locking I do believe. And you could use file locking too. Basically,
just make sure you own the lock when performing an operation on the file
descriptor.
A simplified class would be something like this:
class SyncSerialPort
{
public:
SyncSerialPort( std::string dev )
{
_fd = open( dev.c_str(), O_RDWR ) ;
throw if _fd < 0
pthread_mutex_init( &_lock, NULL ) ;
}
~SyncSerialPort()
{
pthread_mutex_destroy( &_lock ) ;
close( _fd ) ;
}
size_t
read( void* data, size_t size )
{
size_t ret ;
pthread_mutex_lock( &_lock ) ;
ret = read( _fd, data, size ) ;
pthread_mutex_unlock( &_lock ) ;
return ret ;
}
size_t
write( void* data, size_t size )
{
size_t ret ;
pthread_mutex_lock( &_lock ) ;
ret = write( _fd, data, size ) ;
pthread_mutex_unlock( &_lock ) ;
return ret ;
}
private:
int _fd ;
pthread_mutex_t _lock ;
} ;
------------------------------------------------------------------------
_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]