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 the use of Form Authentication and windows Authentication?

1 Answers   NetCity, TCS,


If I update session state, should I lock it, too? Are concurrent accesses by multiple requests executing on multiple threads a concern with session state?

1 Answers  


How do we implement bundling in MVC?

0 Answers   B-Ways TecnoSoft,


we can able to display a MessageBox in asp .net without using any script langages?

8 Answers   ABC,


How you will manage the state of ASP.NET controls?

0 Answers   Sans Pareil IT Services,


Define globalization and localization.

0 Answers  


What is the mvc model?

0 Answers  


Usage of web.configuration ?

3 Answers   Microsoft,


How to transpose rows into columns and columns into rows in a multi-dimensional array ?

1 Answers   Microsoft,


Uses of CLR ?

2 Answers   Mind Tree, PH Software, TCS,


When should I use server transfer and response redirect?

0 Answers  


What are ASP.NET Web Forms? How is this technology different than what is available though ASP (1.0-3.0)?

1 Answers   Siebel Systems,


Categories