Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Threading - lock - how to check lock occurrences

Im using locking object in my application with multiple threads.

How i can check how many times other threads tried to work on locked object, or how much time i wasted on trying to update locked object?

My code is based on best answer here:

Mutliple threads updating array

Edit: code copied over:

float[] bestResult;
object sync = new Object();

lock (sync) 
{
    if (bestResult[0] > calculatedData[0]) {
        bestResult = calculatedData;
    }
}
like image 507
Kamil Avatar asked Feb 18 '23 17:02

Kamil


1 Answers

The System.Diagnostics.Stopwatch class may help you here:

float[] bestResult;
object sync = new Object();
var sw = new System.Diagnostics.Stopwatch();
sw.Start();

lock (sync) 
{
    sw.Stop();
    if (bestResult[0] > calculatedData[0]) {
        bestResult = calculatedData;
    }
}

Console.WriteLine("Time spent waiting: " + sw.Elapsed);
like image 190
FMM Avatar answered Mar 03 '23 19:03

FMM