locked
Threading Issue - lock RRS feed

  • Question

  • Hi,

    I have array of 10 threads executing the same function,

    Inside the function I put some specific lines of code in lock() block, to ensure only one thread accesses it at a time.

    I can see all 10 threads entering the fucntion...no probelm there....

    As sson as lock() block code started, it was executed only once as expected.... no issues there also...

    After the lock block, i expect all 10 thread start on the execution.... but that is not happening. Only one thread is executing the rest of the code at a time...

    why this happening?

    fucntion dosome()

    {

    int i=10;

    lock(_lock) {

    }

    callsoemthing();

    }


    In the above code, all 10 thread enter the statement 'int i...', but 'callsomething' is executed by only one thread...

    Friday, November 9, 2012 9:51 AM

Answers

  • Hi Shanthi,

    Welcome to the MSDN Forum.

    I upload this picture to illustrate this situation:

    You can image this is a kind of aisle, the first part and the third party is enough large for ten people, and the lock party is the a security check point that people can only pass through it one by one.

    This results the third party only have one person at same time.

    This is why.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Friday, November 23, 2012 11:25 AM
    Wednesday, November 14, 2012 10:28 AM

All replies

  • I can guess that "callsomething" is locked by synchronization context. It is possible to instruct CLR to lock declaratively. See http://msdn.microsoft.com/en-us/library/system.runtime.remoting.contexts.synchronizationattribute.aspx.

    I can also suggest you to look at the following book where various synchronization mechanisms are described: http://www.albahari.com/threading/part2.aspx#_Synchronization_Contexts


    Please remember to mark the replies as answers if they help

    Friday, November 9, 2012 11:01 AM
  • Maybe this is what happening in your code

    look at the following sample

    10 thread starting threadstart, each thread mark with id between 0 to 9,

    you can easily see ,when running it, that the dosomthing is called simultaneously by all the 10 threads.

    note that the lock routine is 100milisec while the dosomthing routine is 1000milisec

    if you would reverse the numbers and make the lock 1000milisec and the dosomthing 100milisec you will see that what you described is happening

    I think what is happened in you code is that the lock is X milisec long while dosomthing is less then x/numberofthreads

    so it seems to be synchronous

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication16
    {
        class Program
        {
            static object lo = new object();
    
            static void Main(string[] args)
            {
    
                System.Threading.ThreadPool.SetMinThreads(10, 10);
    
    
                Parallel.For(0, 10,
                    (x) =>
                    {
                        threadstart(x);
    
                    });
    
                Console.ReadLine();
    
            }
    
            static void threadstart(int i)
            {
    
                lock (lo)
                {
                    Console.WriteLine("Thread " + i + " enter the lock");
                    System.Threading.Thread.Sleep(100);
                    Console.WriteLine("Thread " + i + " exit the lock");
    
                }
    
                dosomthing(i);
    
            }
    
            static void dosomthing(int i)
            {
                Console.WriteLine("Thread " + i + " enter dosomthing");
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("Thread " + i + " exit dosomthing");
    
            }
        }
    }


    • Edited by Avi Kadosh Friday, November 9, 2012 11:23 AM spelling
    Friday, November 9, 2012 11:20 AM
  • Hi Shanthi,

    Welcome to the MSDN Forum.

    I upload this picture to illustrate this situation:

    You can image this is a kind of aisle, the first part and the third party is enough large for ten people, and the lock party is the a security check point that people can only pass through it one by one.

    This results the third party only have one person at same time.

    This is why.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Friday, November 23, 2012 11:25 AM
    Wednesday, November 14, 2012 10:28 AM