473,480 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Issue with lock(this) { ... }

All,

I have a multithreaded app (using threadpool). I was under the illusion
that where I used lock( this ) around a segment of code that only one thread
would be allowed access to that segment of code at a time.

The scenario I have is that a part of code creates a document "new.txt" (for
simplicity) and zips it up, I only ever want one "new.txt" so I put this
code inside a lock( this ), however having reviewed my code this does not
act as desired; I see many threads inside this locked section at the same
time :-/

Am I missing something?

MA
Nov 16 '05 #1
4 1537
Max Adams <ru************@hotmail.com> wrote:
I have a multithreaded app (using threadpool). I was under the illusion
that where I used lock( this ) around a segment of code that only one thread
would be allowed access to that segment of code at a time.
For that one instance, yes.
The scenario I have is that a part of code creates a document "new.txt" (for
simplicity) and zips it up, I only ever want one "new.txt" so I put this
code inside a lock( this ), however having reviewed my code this does not
act as desired; I see many threads inside this locked section at the same
time :-/

Am I missing something?


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.

Also, have a look at

http://www.pobox.com/~skeet/csharp/threads/
and in particular
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi,

It's not a good idea to use lock( this ), as it block the entire instance,
create one instance member and lock it,

take a look at:
http://www.yoda.arachsys.com/csharp/.../locking.shtml

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Max Adams" <ru************@hotmail.com> wrote in message
news:O9**************@TK2MSFTNGP15.phx.gbl...
All,

I have a multithreaded app (using threadpool). I was under the illusion
that where I used lock( this ) around a segment of code that only one thread would be allowed access to that segment of code at a time.

The scenario I have is that a part of code creates a document "new.txt" (for simplicity) and zips it up, I only ever want one "new.txt" so I put this
code inside a lock( this ), however having reviewed my code this does not
act as desired; I see many threads inside this locked section at the same
time :-/

Am I missing something?

MA

Nov 16 '05 #3
Blocking across the entire instance is often the desired effect, I
think the real concern of lock(this) is code outside of the class can
also lock on the object reference, which can cause problems.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 21 Oct 2004 11:16:40 -0400, "Ignacio Machin \( .NET/ C# MVP
\)" <ignacio.machin AT dot.state.fl.us> wrote:
Hi,

It's not a good idea to use lock( this ), as it block the entire instance,
create one instance member and lock it,

take a look at:
http://www.yoda.arachsys.com/csharp/.../locking.shtml

cheers,


Nov 16 '05 #4
And besides, lock(this) *doesn't* block across the entire instance. It simply blocks other callers of lock(this) (or external objects that decide to synchronize of that particular instance).

I could theoretcally use lock(this) to only protect one field in my object.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<2h********************************@4ax.com>

Blocking across the entire instance is often the desired effect, I
think the real concern of lock(this) is code outside of the class can
also lock on the object reference, which can cause problems.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
437
by: n_o_s_p_a__m | last post by:
I have seen some debate but am not clear on what the problematic implications of the expression lock(this) are. Microsoft seems to advocate it, but others seem to be against it. Pros and cons? ...
5
4887
by: norbert.thek | last post by:
Hi Can somebody tell me if I'm right or not The attribute Is like an lock(this) over the Method where i put this Attribute?! If I have two methods like:
2
3540
by: yaron | last post by:
Hi, Does MethodImpl Synchronized attribute is like lock(this) ? i mean that a call to Monitor.PulseAll(this) from a Synchronized method will trigger other thread that call to Monitor.Wait(this)...
0
7044
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.