Re: select() and multi-threading
- From: "Maxim Yegorushkin" <maxim.yegorushkin@xxxxxxxxx>
- Date: 22 Jun 2006 07:59:08 -0700
Robert Latest wrote:
I'm writing an app that does some writing to and reading from an
external device. Since I don't know when data comes in, I'd like to do
the reading in a separate thread which accumulates data in a queue to be
processed by the main application whan it has time.
I've got this working now, but the reading and writing of the file
descriptor is in no way synchronized -- the "listener" thread simply
blocks on a read() until some data comes in, then puts it in the queue.
My hunch is that this will eventualy break because of a collision of a
read() and write() from the two threads.
Question 1: Can it?
On the assumption that it can, I thought about letting the "listener"
block on a select() on the fd, lock a mutex when select() returns, do
the read() and unlock the mutex when finished. Of course the write() in
the other thread is protected by the same mutex.
Question 2: Is this safe, or are there still collisions possible
(between write() and select())?
This question has been answered recently here
http://groups.google.com/group/comp.os.linux.networking/msg/da8c7a03acfa1d18
(show option -> view thread)
Question 3: How would you do it?
Learn more about IO strategies
http://www.kegel.com/c10k.html#strategies
Use nonblocking IO. Do reading and writing in the same thread.
.
- Follow-Ups:
- Re: select() and multi-threading
- From: Robert Latest
- Re: select() and multi-threading
- References:
- select() and multi-threading
- From: Robert Latest
- select() and multi-threading
- Prev by Date: Re: EADDRINUSE when it shouldnt be... can sockaddr be reused?
- Next by Date: Re: bind() failing with EADDRINUSE
- Previous by thread: select() and multi-threading
- Next by thread: Re: select() and multi-threading
- Index(es):
Relevant Pages
|
Loading