Davio
Davio

Reputation: 4737

Lock used in different methods

I have a question about locking and whether I'm doing it right.

In a class, I have a static lock-object which is used in several methods, assume access modifiers are set appropriately, I won't list them to keep it concise.

class Foo
{ 
   static readonly object MyLock = new object();

   void MethodOne()
   {
       lock(MyLock) { 
          // Dostuff
       }
   }

   void MethodTwo()
   {
       lock(MyLock) { 
          // Dostuff
       }
   }
}

Now, the way I understand it, a lock guarantees only one thread at a time will be able to grab it and get into the DoStuff() part of one method.

But is it possible for the same thread to call MethodOne() and MethodTwo() at the same time? Meaning that he uses the lock he has gotten for both methods?

My intended functionality is that every method in this class can only be called by a single thread while no other method in this class is currently executing.

The underlying usage is a database class for which I only want a single entry and exit point. It uses SQL Compact, so if I attempt to read protected data I get all sorts of memory errors.

Let me just add that every once and a while a memory exception on the database occurs and I don't know where it's coming from. I thought it was because of one thread doing multiple things with the database before completing things, but this code seems to work like it should.

Upvotes: 0

Views: 1266

Answers (4)

M Afifi
M Afifi

Reputation: 4795

There are a few things that can be answered here.

But is it possible for the same thread to call MethodOne() and MethodTwo() at the same time? Meaning that he uses the lock he has gotten for both methods?

No, a thread has a single program counter, its either in MethodOne() or in MethodTwo(). If however you have something as follows,

public void MethodThree()
{
    lock (MyLock)
    {
        MethodOne();
        MethodTwo();
    }
}

That will also work, a thread can acquire the same lock multiple times. Just watch out for what you're doing as you can easily get into a deadlock as the code becomes more complex.

My intended functionality is that every method in this class can only be called by a single thread while no other method in this class is currently executing.

The underlying usage is a database class for which I only want a single entry and exit point. It uses SQL Compact, so if I attempt to read protected data I get all sorts of memory errors.

I don't really understand why, but if you think you need to do this because you're using SqlCompact, you're wrong. You should be using transactions which are supported on SqlCe.

E.g.

using (var connection = new SqlCeConnection())
using (var command = new SqlCeCommand())
using (var transaction = conn.BeginTransaction())
{
    command.Transaction = transaction;
    command.ExecuteNonQuery();
    transaction.Commit();
}

Upvotes: 0

Azodious
Azodious

Reputation: 13872

But is it possible for the same thread to call MethodOne() and MethodTwo() at the same time?

No. Same thread can't call both the methods at same time whether lock is used on not.

lock(MyLock) 

It can be understood as following:

MyLock object has a key to enter itself. a thread (say t1) who accesses it first gets it. Other threads will have to wait until t1 releases it. But t1 can call another method and will pass this line as it already has acquired lock.

But, at the same time calling both the methods ... not possible by single thread. Not in current programming world.

the way I understand it, a lock guarantees only one thread at a time will be able to grab it and get into the DoStuff() part of one method.

Your understanding is correct but remember that threads are used to do parallel execution but the execution within a thread is always sequential.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

The only way for a thread inside Dostuff of the running MethodOne to call MethodTwo is for the Dostuff of the MethodOne to make the call to MethodTwo. If this is not happening (i.e. methods in your "mutually locked" group do not call each other), you are safe.

Upvotes: 1

Oded
Oded

Reputation: 499002

But is it possible for the same thread to call MethodOne() and MethodTwo() at the same time?

It is not possible for a single thread to call anything at the same time.

In a multithreaded application, this can happen - the methods can be called simultaneously, but the // Dostuff sections can only be accessed sequentially.

My intended functionality is that every method in this class can only be called by a single thread while no other method in this class is currently executing.

Then don't use additional threads in your application - just have the main one and don't use extra ones.

Upvotes: 1

Related Questions