answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a standard lock plus double check to create a critical section around a variable access?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which variable is permitted or denied by a standard access control list?

destination mac address


What is difference between struct variable and class object?

structure variable can access the variable but class object can access the function also


What are the requirements of mutual exclusion?

The requirements for mutual exclusion are as follows 1. Only one process at a time is allowed to enter mutual its critical section for a resource. 2. A process that halts in its non-critical section must do so without interferring with other processes. 3. It must no be possible for a process requirning access to a critical section to be delayed indefinitly. 4. A process must not be delayed if it wishes to enter a critical section if there is no other process using it. 5. No assumptions are made about relitive process speeds or number of processes. 6, A process remains in it critical section for a finite amount of time only.


What is is null in Microsoft access?

Variable


Which is public to all access class or struct or function or variable?

The default access specifier for a class is private. The default access specifier for a struct is public. It does not matter if it is a function or a variable.


What is an example of a 'mutual exclusion'?

mutual exclusion makes sure that concurrent processes access shared resources or data in a serialized way. if a process say Pi is executing in its critical section , then no other processes can be executing in their critical section.


What is an access specifier?

An access specifier is a keyword applied to a variable, or method, which indicates which parts of the program are permitted to access it.


What is the description of the critical section problem?

The Critical section problem occurs when the processors are in a Network. For eg consider that 3 processors share same database at a time, if one processor wants to update the database then no other process will get the access to that database. This point is called as critical section. Temporarily the connection between the other 2 system will be dropped.


How do named access lists differ from standard access lists?

Named access lists can be identified by alphanumeric names while standard access lists cannot


What is the key word used to access the variable declared as global variable in another file?

r+ is the keyword


Where is constant variable stored?

Random Access Memory (RAM)


What is algorithm to write algorithm to the program to access a pointer variable in structure?

Here is the algorithm of the algorithm to write an algorithm to access a pointer in a variable. Algorithmically.name_of_the_structure dot name_of_the _field,eg:mystruct.pointerfield