none
How to find out if a function is being executed by two threads? RRS feed

  • Question

  • What is the best way to find out if a function is being executed by two different threads at the same time?
    I believe there is a way to check the threadID of the thread currently executing the function
    BUT
    *Will that thread always have a constant ID through the life time of the application?
    *If using threadpool, what is the guarantee same thread will be pulled out to execute the function?

    Is there any better way to find it out?


    Tuesday, March 2, 2010 3:07 PM

Answers

  • Can't you simply have a shared variable that you increment on entry to the method and decrement before leaving (in a synchronized, thread safe way of course)? I don't see how the thread ID would help you here.


    Mattias, C# MVP
    • Marked as answer by eryang Tuesday, March 9, 2010 3:30 AM
    Tuesday, March 2, 2010 4:32 PM
    Moderator
  • There are several synchronization constructs that you can use to check if two or more threads are executing the same method concurrently. You could use the AutoResetEvent class as a gate keeper that throws an exception if a second thread tries to enter the method when another thread is already executing.
    You could also have a class scoped variable that is increased as a thread enters the method and decreased on exit. You need som logic to store the highest value stored by the variable in order to evaluate afterwards. You would need to rely on the Interlocked class for increasing/decreasing the value.

    		static int max;
    		static int current;
    		static object sync = new object();
    
    		static void Proc(object state)
    		{
    			int num = Interlocked.Increment(ref current);
    			lock (sync)
    			{
    				if (num > max)
    				{
    					max = num;
    				}
    			}
    
    			// Do what ever work you need to do here
    			// ...
    
    			Interlocked.Decrement(ref current);
    		}
    



    As you already figured out the ThreadID cannot be used since the thread pool uses any number of threads and there is no guarantee that the same woker thread will be used to execute a method at all times. One thread will however have the same ID during the lifetime of the thread.

    If you need to be concerned about how may threads are in a certain method at runtime this could indicate a design flaw. Rather, if the method exposes resources that may only be accessed by one thread at a time you need to lock that resource.

    /Calle


    - Still confused, but on a higher level -
    • Marked as answer by eryang Tuesday, March 9, 2010 3:30 AM
    Tuesday, March 2, 2010 4:51 PM

All replies

  • Can't you simply have a shared variable that you increment on entry to the method and decrement before leaving (in a synchronized, thread safe way of course)? I don't see how the thread ID would help you here.


    Mattias, C# MVP
    • Marked as answer by eryang Tuesday, March 9, 2010 3:30 AM
    Tuesday, March 2, 2010 4:32 PM
    Moderator
  • There are several synchronization constructs that you can use to check if two or more threads are executing the same method concurrently. You could use the AutoResetEvent class as a gate keeper that throws an exception if a second thread tries to enter the method when another thread is already executing.
    You could also have a class scoped variable that is increased as a thread enters the method and decreased on exit. You need som logic to store the highest value stored by the variable in order to evaluate afterwards. You would need to rely on the Interlocked class for increasing/decreasing the value.

    		static int max;
    		static int current;
    		static object sync = new object();
    
    		static void Proc(object state)
    		{
    			int num = Interlocked.Increment(ref current);
    			lock (sync)
    			{
    				if (num > max)
    				{
    					max = num;
    				}
    			}
    
    			// Do what ever work you need to do here
    			// ...
    
    			Interlocked.Decrement(ref current);
    		}
    



    As you already figured out the ThreadID cannot be used since the thread pool uses any number of threads and there is no guarantee that the same woker thread will be used to execute a method at all times. One thread will however have the same ID during the lifetime of the thread.

    If you need to be concerned about how may threads are in a certain method at runtime this could indicate a design flaw. Rather, if the method exposes resources that may only be accessed by one thread at a time you need to lock that resource.

    /Calle


    - Still confused, but on a higher level -
    • Marked as answer by eryang Tuesday, March 9, 2010 3:30 AM
    Tuesday, March 2, 2010 4:51 PM