Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

What is the best way to do thread locking in .NET?Thanks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
linuxfedora@yahoo.com.hk - 10 Sep 2007 05:46 GMT
if i have a Queue named testQ

Object lockQ = new Object();

testQ will be modified by:

void AddToQueue(string text)
{
lock(lockQ )
{
testQ.Enqueue(text);
}
}

string RemoveFromQueue()
{
lock(lockQ )
{
string text = (string)testQ.Dequeue();
}
return text ;
}

void PrintInfo()
{
lock(lockQ)
{
for(int i=0;i<testQ.Count;i++)
{
Console.WriteLine("String=" + RemoveFromQueue());
}
}
}

How to ensure that the PrintInfo is not affected if when it is
printing the info, a new text is added to the Queue in AddToQueue
function? And the most effective way to do it is?Thanks
Göran Andersson - 10 Sep 2007 06:12 GMT
> if i have a Queue named testQ
>
[quoted text clipped - 33 lines]
> printing the info, a new text is added to the Queue in AddToQueue
> function? And the most effective way to do it is?Thanks

You are pretty close. You have to declare the text variable outside the
scope of the lock to be able to return it:

string RemoveFromQueue() {
   string text;
   lock(lockQ ) {
      text = (string)testQ.Dequeue();
   }
   return text;
}

You don't have to call RemoveFromQueue in the PrintInfo method, you can
use testQ.Dequeue as it's protected inside the lock block.

You can't use testQ.Count that way in the loop, though. As both i and
testQ.Count are changing in the loop you will only show half of the
items. Just loop as long as there are items in the queue:

void PrintInfo() {
   lock(lockQ) {
      while (testQ.Count > 0) {
         Console.WriteLine("String=" + (string)testQ.Dequeue());
      }
   }
}

I would suggest that you put these methods, the testQ variable and the
lock variable in a class by themselves, so that the queue and the lock
variable are isolated from the rest of the code. That way the queue
can't be manipulated by any other code by mistake.

Signature

Göran Andersson
_____
http://www.guffa.com

linuxfedora@yahoo.com.hk - 10 Sep 2007 09:26 GMT
On 9 10 ,   1 12 , G?ran Andersson <gu...@guffa.com> wrote:
> linuxfed...@yahoo.com.hk wrote:
> > if i have a Queue named testQ
[quoted text clipped - 73 lines]
>
> -         -

Thanks.
Jon Skeet [C# MVP] - 10 Sep 2007 19:07 GMT
G?ran Andersson <guffa@guffa.com> wrote:
> You are pretty close. You have to declare the text variable outside the
> scope of the lock to be able to return it:
[quoted text clipped - 6 lines]
>     return text;
> }

Not really - I'd say it would be cleaner to return it directly from
inside the lock:

string RemoveFromQueue()
{
   lock (lockQ)
   {
       return (string) testQ.Dequeue();
   }
}

This will still release the lock appropriately.

<snip>

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

linuxfedora@yahoo.com.hk - 11 Sep 2007 03:34 GMT
How about:
There are 2 threads, named thread1 and thread2, they will also access
the method in test1 named testMethod as below:

thread1 will call testMethod(10);
thread2 will call testMethod(20);

public void testMethod(int count)
{
lock(lockData)
{
for(int i=0;i<count;i++)
{
Console.WriteLine("i=" + i);
}
}
}
}

is that possible to lock the count varaible? because the function
parameter is not in the LOCK?Thanks
Peter Duniho - 11 Sep 2007 05:33 GMT
> [...]
> is that possible to lock the count varaible? because the function
> parameter is not in the LOCK?Thanks

There is no need to lock the count variable, since each thread has its
own instance of the variable.  The variable isn't shared between threads.

Pete
linuxfedora@yahoo.com.hk - 11 Sep 2007 11:34 GMT
Göran Andersson - 11 Sep 2007 11:54 GMT
>> [...]
>> is that possible to lock the count varaible? because the function
[quoted text clipped - 4 lines]
>
> Pete

Also:

You don't lock the object, you lock the code. The lock prevents another
thread to enter a code block that is locked on the same object. The
object that you use in the lock statement is just an identifier for the
lock, it doesn't protect the object.

You can't use a value type in a lock statement. The value would be boxed
in a new object, so another lock on the same value would lock on a
separate new object.

Signature

Göran Andersson
_____
http://www.guffa.com

Jon Skeet [C# MVP] - 11 Sep 2007 20:37 GMT
> Also:
>
> You don't lock the object, you lock the code. The lock prevents another
> thread to enter a code block that is locked on the same object. The
> object that you use in the lock statement is just an identifier for the
> lock, it doesn't protect the object.

While I see your point, I'd say you lock the *reference* rather than
either the object or the code.

In particular, the phrase "lock the code" (without reading the rest of
the text) would suggest that no other thread can execute the same code
at the same time. It can, if it ends up locking on a different
reference.

The explanation is spot on, of course.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Göran Andersson - 11 Sep 2007 21:04 GMT
>> Also:
>>
[quoted text clipped - 10 lines]
> at the same time. It can, if it ends up locking on a different
> reference.

I see what you mean. I'd say that it doesn't lock the reference either,
it just locks the lock. :)

> The explanation is spot on, of course.

Thanks. :)

Signature

Göran Andersson
_____
http://www.guffa.com

Peter Duniho - 11 Sep 2007 21:12 GMT
>>> Also:
>>>
[quoted text clipped - 7 lines]
> I see what you mean. I'd say that it doesn't lock the reference either,
> it just locks the lock. :)

I think what you call it is not nearly so important as understanding
that like a critical section or other synchronization mechanism, it only
protects data when all code that accesses the data cooperates and uses
the same mechanism.

Code isn't locked, the object isn't locked, nothing is literally locked.
 What does happen is that the synchronization mechanism in use ensures
only a single thread at a time has access to the resource.

Without cooperation between all of the relevant code, this doesn't happen.

IMHO, your original elaboration made this point reasonably well.  :)
There's probably no need to worry about whether we are saying that the
variable is locked or the code is locked or the object is locked or
the...  :)

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.