what is the lock keyword ?

S

Steve B.

Hi,

I'm wondering what exactly is the lock keyword used for.

I'm building an application that call some win32 apis. Theses apis enumerate
registry key(RegEnumKeyEx). I can't use .Net ones because I'm building
CF.Net app and registry is not implemented
To enumerate registry keys I need to call the RegEnumKey until there is no
more item.
I added the lock(this) statement around the code that do that, in order to
be sure my enumeration won't be interfered with other thread (that should be
write in registry).

Is this the rigth usage of the lock object ?

Thanks,
Steve
 
L

Lloyd Dupont

I added the lock(this) statement around the code that do that, in order to
be sure my enumeration won't be interfered with other thread (that should
be write in registry).
well on one hand I am tempted to say yes, exactly.
given a lock object obj, only one thread at a time could execute an excution
guarded by this lock statment as in:
lock(obj)
{
// atomic statment, that is only one thread at a tine could enter
lock(obj)
Something();
SomethingElse();
}

On the other hand, beware the lock is around the object value. so this would
NOT work:
void DoThread()
{
object obj = new object(); // error: new object everytime => no common
lock
lock(obj)
Something();
}

but this would:
object obj = new object();
void DoThread()
{
lock(obj) // good: common lock
Something();
}
 
S

Steve B.

Thanks for these explanations...




Lloyd Dupont said:
well on one hand I am tempted to say yes, exactly.
given a lock object obj, only one thread at a time could execute an
excution guarded by this lock statment as in:
lock(obj)
{
// atomic statment, that is only one thread at a tine could enter
lock(obj)
Something();
SomethingElse();
}

On the other hand, beware the lock is around the object value. so this
would NOT work:
void DoThread()
{
object obj = new object(); // error: new object everytime => no common
lock
lock(obj)
Something();
}

but this would:
object obj = new object();
void DoThread()
{
lock(obj) // good: common lock
Something();
}
 
T

Tiberiu Covaci[MCAD.NET]

About lock msdn says:
A lock statement of the form:

lock (x)

is equivalent to:

Monitor.Enter(x);

try { ... }

finally { Monitor.Exit(x); }

however, x is only evaluated once.

Now. If you want to be sure that only your application is gaining access to
the registry, that wouldn't work, simply because x is local to your
AppDomain, so you will have to find another solution.

Tibi
 
S

Steve B.

In our case it won't be a problem, since it is on a mobile device and there
will be only one app running...
I just wanted to be sure to preserve thread safe access to my reg object
 
J

Jon Skeet [C# MVP]

Steve B. said:
I'm wondering what exactly is the lock keyword used for.

I'm building an application that call some win32 apis. Theses apis enumerate
registry key(RegEnumKeyEx). I can't use .Net ones because I'm building
CF.Net app and registry is not implemented
To enumerate registry keys I need to call the RegEnumKey until there is no
more item.
I added the lock(this) statement around the code that do that, in order to
be sure my enumeration won't be interfered with other thread (that should be
write in registry).

Is this the rigth usage of the lock object ?

Aside from the other replies, I would suggest you *don't* lock on this.
See http://www.pobox.com/~skeet/csharp/threads/lockchoice.shtml for the
reason why.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top