Re: CCriticalSection - does my thread already have a lock?
- From: "Dan Baker" <dbmail>
- Date: Thu, 20 Jul 2006 10:35:19 -0600
"Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
news:1g9vb29v2okt3tlbqbnt3kjiff38h5mslh@xxxxxxxxxx
On Thu, 20 Jul 2006 09:08:18 -0600, "Dan Baker" <dbmail> wrote:
My current design is as follows:
I have several worker threads waiting on an IOCP.
When a worker thread wakes up from the IOCP it does the following:
1) locks down the list of contexts
2) hunts for the appropriate context, validates that the context is still
ok
3) unlocks the list of context (to avoid deadlock)
4) locks the context (which could wait on another thread that is already
processing a packet)
The confusion happens at this point. The "other" worker thread that had
the
context locked could determine that the connection needs to be closed (due
to a number of reasons). If this other worker thread determines to close
the connection, it also needs to invalidate the context that the above
thread is now waiting to lock. As soon as this other worker thread
unlocks
the context, the above thread will wake up from the blocked lock call,
with
a context which has become "invalid" (closed).
I will look in reference counting. Any other suggestions would be
welcome.
Couldn't the "context" maintain a flag indicating its state? Or you might
be able to maintain a mapping between context addresses and (invalid)
state. This former would of course be performed under the protection of
the
context lock, while the latter would require a more global type of lock if
the context lock is just for a single context.
BTW, it sounds like your locking protocol has a sort of race, in which the
events could proceed like this:
1. IOCP M fires, thread X finds its context
2. IOCP N fires, thread Y finds its context
3. Thread Y locks its context
4. Thread X locks its context
If order is important, you might need to use a queue.
I think you are right about the race condition. But, I don't really see a
solution to the problem. Since there are multiple threads waiting on the
IOCP, and multiple packets can be queued to the IOCP from the same socket.
The race condition exists between the two threads that wake up from the
IOCP. Is there a known solution to this? Does everyone simply ignore the
race condition?
Could proceed like this:
1. IOCP M fires, thread X wakes up (but a timeslice happens, and the thread
goes to sleep)
2. IOCP N fires, thread Y wakes up
3. Thread Y finds its context and locks it
4. Thread X is too late. The packets are processed in the wrong order.
I would be glad to use a queue, but I don't see where I could add it.
DanB
.
- Follow-Ups:
- Re: CCriticalSection - does my thread already have a lock?
- From: Doug Harrison [MVP]
- Re: CCriticalSection - does my thread already have a lock?
- References:
- CCriticalSection - does my thread already have a lock?
- From: Dan Baker
- Re: CCriticalSection - does my thread already have a lock?
- From: Alexander Grigoriev
- Re: CCriticalSection - does my thread already have a lock?
- From: Dan Baker
- Re: CCriticalSection - does my thread already have a lock?
- From: Doug Harrison [MVP]
- CCriticalSection - does my thread already have a lock?
- Prev by Date: Re: WinForms vs MFC
- Next by Date: Re: vc++ express
- Previous by thread: Re: CCriticalSection - does my thread already have a lock?
- Next by thread: Re: CCriticalSection - does my thread already have a lock?
- Index(es):
Relevant Pages
|