Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Write a standard lock() plus double check to create a
critical section around a variable access?



Write a standard lock() plus double check to create a critical section around a variable access?..

Answer / debmalya kundu

using System;
using System.Text;

using System.Threading;

namespace thread01

{

public class Counter

{

private int _count=0;

private int _even=0;

public int Count { get { return _count; } }

public int EvenCount { get { return _even; } }




private Object theLock = new Object();



public void UpdateCount()

{

lock (theLock)

{

_count = _count + 1;

if (Count % 2 == 0) // An even number

{

_even += 1;


}

}

}

}

class Program

{

static void Main(string[] args)

{

Counter count = new Counter();

ParameterizedThreadStart starter = new
ParameterizedThreadStart(Program.UpdateCount);

Thread[] threads = new Thread[10];


for (int x = 0; x < 10; ++x)

{

threads[x] = new Thread(starter);

threads[x].Start(count);

}

for (int y = 0; y < 10; ++y)

{

threads[y].Join();

}

Console.WriteLine("Total: {0} - Even: {1}",
count.Count,count.EvenCount);

Console.ReadKey();

Console.ReadKey();

}

static void UpdateCount(object param)

{

Counter count = (Counter)param;

for (int z = 1; z <= 100000; ++z)

{

count.UpdateCount();

}

}

}

}

Is This Answer Correct ?    3 Yes 0 No

Post New Answer

More ASP.NET Interview Questions

What is the difference between table and query?

0 Answers  


To make Cache item dependent on a file,directory or other cached item ,you should create an instance of which clause?

1 Answers  


what is caching,session? when & what is used mostly in which situtations, how they r implemented in real time? tell with example?

1 Answers   Satyam,


How much is the pay-for-use service if I chose not to use microsoft-sponsored advertising?

0 Answers  


i have 3+ exp in .net? i am going interview now but they asked me do you know any TOOL? which tool will i study please refer me?

0 Answers   Wipro,


Caching techniques in .NET?

2 Answers   Microsoft,


What are the uses of reflection?

0 Answers  


Define cookie.

0 Answers  


What is http response header?

0 Answers  


Explain the difference between globalization and localization techniques

0 Answers  


Will the asp.net validators run in server side or client side?

0 Answers  


what is diffgram ?

3 Answers  


Categories