简体   繁体   中英

C# Multithreading with Lock

I am a beginner to C#. I am taking a look at sample of multithreading.

I have set up a multithreaded call to method DoTransactions() , which in turn will generate a random number as amount , and call method Withdraw() to deduct salary by amount .

1) What I don't understand is that why the writer chose to lock Object thisLock instead of locking salary? I saw some others also lock thread in this way by declaring an object and locking it.

2) Sometimes I cannot debug the methods called when the other threads were running. (by pressing F10/F11). (eg methods Withdraw() ). Any reason for this?

Department dep = new Department(1000); 
Thread t = new Thread(new ThreadStart(dep.DoTransactions));   //set up 1000 threads.

class Department
    {
        private Object thisLock = new Object();
        int salary = 10000;

        int Withdraw(int amount)
        {
            lock (thisLock)
            {
                if (salary >= amount)
                {
                    salary = salary - amount;
                    return amount;
                }
            }
        }
  1. You can lock only on class (reference type) while Salary is an int which is value type.

MSDN :

The expression of a lock statement must denote a value of a reference-type. No implicit boxing conversion (Section 6.1.5) is ever performed for the expression of a lock statement, and thus it is a compile-time error for the expression to denote a value of a value-type.

http://msdn.microsoft.com/en-us/library/aa664735(v=vs.71).aspx

  1. What do you mean you can't debug? Probably the debugger jumps between threads.

The lock is used so that only 1 thread will access that bit of code at any time. And to enable the sync between the threads they must take turns locking an object which is commonly available to all threads.

What you put in a lock statement (like lock (thisLock) ) is not the data being locked . That cannot be the case if your think about it: If you wanted to modify two variables, which one would you put in the lock ? (Answer: No answer exists. Impossible.).

Instead, you put in a reference to an object that will always be locked on by convention when accessing data that is logically protected by the lock.

When debugging multithreaded applications use breakpoints to force looking at specific code lines. You can not rely on your stepping into and stepping over to work sequentially.

In answer to your second question:

It may be that you need to first choose "break all" (click the Break All button) to stop running all threads, and then go to "Debug|Windows|Threads" to see a list of all the threads you can debug. (Each one has a separate stack frame, of course.)

From there you can double-click a thread to see if there's any source code for it. (Some will be "internal" threads that you didn't start, and these won't have source code you can see. Just keep clicking the threads until you find an interesting one. ;)

Other than that, set breakpoints in code which the thread you're interested in will execute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM