Home All Groups Group Topic Archive Search About

Re: framework 1.1 & Queue class

Author
24 Apr 2007 1:30 PM
Brian Gideon
On Apr 24, 2:59 am, "oscar.acostamonte***@googlemail.com"
<oscar.acostamonte***@googlemail.com> wrote:
> Hello:
> Also you can use a sync object, and place queue acces code in a lock
> statement.
> object o = new object();
> lock(o){
>   //Queue operations...}
>
> Best regards.
> Oscar Acosta

Just to clarify...the object used in the lock statement must be the
same in every thread to guarentee thread-safety.  It's difficult to
ascertain what your intention from the code was precisely, but one way
to read it is that the variable o is local and thus would be different
on every thread.

Author
26 Apr 2007 7:12 AM
oscar.acostamontesde@googlemail.com
On Apr 24, 3:30 pm, Brian Gideon <briangid***@yahoo.com> wrote:
Show quote
> On Apr 24, 2:59 am, "oscar.acostamonte***@googlemail.com"
>
> <oscar.acostamonte***@googlemail.com> wrote:
> > Hello:
> > Also you can use a sync object, and place queue acces code in a lock
> > statement.
> > object o = new object();
> > lock(o){
> >   //Queue operations...}
>
> > Best regards.
> > Oscar Acosta
>
> Just to clarify...the object used in the lock statement must be the
> same in every thread to guarentee thread-safety.  It's difficult to
> ascertain what your intention from the code was precisely, but one way
> to read it is that the variable o is local and thus would be different
> on every thread.

Yes, you're rigth, code is not clear. Object o should be a class
memeber, with class scope, so it is always locking on same object.
public class Queue{
private static readonly object o = new object();

private void ReadQueue(){
  lock(o){
      int nValue = (int)localQueue.Dequeue();
      Console.WriteLine("Read from Queue: {0}", nValue);
  }
}
}

Best regards
Oscar Acosta

AddThis Social Bookmark Button