Re: .net 2.0 : looking for a "best practice" for multi threading jobs
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Mon, 26 Feb 2007 17:59:34 -0000
Steve B. <steve_beauge@xxxxxxxxxxxx> wrote:
I'm getting troubles in implementing the producer/consumer pattern for
multiple parralel threads...
Here where I am (a project class)... the code is simplified for better
read...
In some conditions, all threads are "blocked" in the GetNectAction() method
waiting in the Monitor.WaitOne method...
Do you have any idea what is wrong ?
Well, a few points:
1) You don't usually need one lock per variable. I tend to have one
lock for a whole instance of a class unless I have good reason to have
more granularity.
2) Point 1 opens you up for bugs where you lock on the wrong thing: for
example, SetStopped locks on m_numberOfThreadsLockObject to set
m_stopped, but the Stopped property locks on m_stoppingLockObject to
fetch it.
3) In Stop, you pulse all the waiting threads before setting the
stopping flag. Any thread which is still working when the pulse occurs
but requests a next action before m_stopping is set to true will wait
forever.
That's just what I could spot in a very quick scan. It would be much
better if I could run the code.
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- References:
- .net 2.0 : looking for a "best practice" for multi threading jobs
- From: Steve B.
- Re: .net 2.0 : looking for a "best practice" for multi threading jobs
- From: Michael Nemtsev
- Re: .net 2.0 : looking for a "best practice" for multi threading jobs
- From: Steve B.
- .net 2.0 : looking for a "best practice" for multi threading jobs
- Prev by Date: Re: VS 2005 on Windows Vista Business
- Next by Date: Re: Available "Initial Catalog" in given Data Source (sql)
- Previous by thread: Re: .net 2.0 : looking for a "best practice" for multi threading jobs
- Next by thread: Unit Test Framework for .NET 2.0?
- Index(es):
Relevant Pages
|