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?s the difference between Codebehind="MyCode.aspx.cs" andSrc="MyCode.aspx.cs"?

5 Answers   SLC Ltd,


What is the need of client side and server side validation ?

1 Answers   Keane India Ltd,


Explain Page life cycle

8 Answers   DELL,


What is directive in asp net?

0 Answers  


What are type/key pairs in client script registration?

0 Answers  


Can we use multiple forms in single asp.net application?

3 Answers   Polaris,


How to integrate angular 8 with asp.net mvc 5? : Asp.Net MVC

0 Answers  


Describe and In Process Vs Out of Process component. Which is faster?

2 Answers   Syntax Softtech,


How do you do exception management?

1 Answers   Accenture, BirlaSoft,


How to use one project files into another project?

4 Answers   HP,


Define Query Interface,Adref,Release?

1 Answers   Microsoft,


Where would you use an iHTTPModule, and what are the limitations of any approach you might take in implementing one?

1 Answers  


Categories