2024年1月31日发(作者:)

using System;using ;using ing;namespace _Mutex{ class BankAccount { public int Balance { get; set; } class Program { static void Main(string[] args) { // create the bank account instance BankAccount account = new BankAccount(); // create the mutex Mutex mutex = new Mutex(); // create an array of tasks Task[] tasks = new Task[10]; for (int i = 0; i < 10; i++) { // create a new task tasks[i] = new Task(() => { // enter a loop for 1000 balance updates for (int j = 0; j < 1000; j++) { // acquire the mutex bool lockAcquired = e(); try { // update the balance e = e + 1; } finally { // release the mutext if (lockAcquired) eMutex(); } } }); // start the new task tasks[i].Start(); } // wait for all of the tasks to complete l(tasks); // write out the counter value ine("Expected value {0}, Balance: {1}", 10000, e); // wait for input before exiting ine("Press enter to finish"); ne(); } } }}等待多处代码同步——Mutex::WaitAll()using System;using ing;

using ing;using ;namespace Listing_12{ class BankAccount { public int Balance { get; set; } } class Listing_12 { static void Main(string[] args) { // create the bank account instances BankAccount account1 = new BankAccount(); BankAccount account2 = new BankAccount(); // create the mutexes Mutex mutex1 = new Mutex(); Mutex mutex2 = new Mutex(); // create a new task to update the first account Task task1 = new Task(() => { // enter a loop for 1000 balance updates for (int j = 0; j < 1000; j++) { // acquire the lock for the account bool lockAcquired = e(); ; try { // update the balance e++; } finally { if (lockAcquired) eMutex(); } } }); // create a new task to update the first account Task task2 = new Task(() => { // enter a loop for 1000 balance updates for (int j = 0; j < 1000; j++) { // acquire the lock for the account bool lockAcquired = e(); try { // update the balance e += 2; } finally { if (lockAcquired) eMutex(); } } }); // create a new task to update the first account Task task3 = new Task(() => { // enter a loop for 1000 balance updates for (int j = 0; j < 1000; j++) { // acquire the locks for both accounts bool lockAcquired = l(new WaitHandle[] { mutex1, mutex2 }); try

static void Main(string[] args) { // create the reader-writer lock ReaderWriterLockSlim rwlock = new ReaderWriterLockSlim(); // create a cancellation token source CancellationTokenSource tokenSource = new CancellationTokenSource(); // create some shared data int sharedData = 0; // create an array of tasks Task[] readerTasks = new Task[5]; for (int i = 0; i < ; i++) { // create a new task readerTasks[i] = new Task(() => { while (true) { // acqure the read lock eadLock(); // we now have the lock ine("Read lock acquired - count: {0}", tReadCount); // read the shared data ine("Shared data value {0}", sharedData); // wait - slow things down to make the example clear e(1000); // release the read lock adLock(); ine("Read lock released - count {0}", tReadCount); // check for cancellation fCancellationRequested(); } }, ); // start the new task readerTasks[i].Start(); } Task[] writerTasks = new Task[2]; for (int i = 0; i < ; i++) { writerTasks[i] = new Task(() => { while (true) { // acquire the upgradeable lock pgradeableReadLock(); // simulate a branch that will require a write if (true) { // acquire the write lock riteLock(); // print out a message with the details of the lock ine("Write Lock acquired - waiting readers {0},writers {1}, upgraders {2}", gReadCount, gWriteCount, gUpgradeCount); // modify the shared data sharedData++; // wait - slow down the example to make things clear e(1000); // release the write lock iteLock(); } // release the upgradable lock gradeableReadLock(); // check for cancellation fCancellationRequested(); }

using System;using ing;using ;namespace Listing_13{ class Listing_13 { static void Main(string[] args) { // declare the name we will use for the mutex string mutexName = "myApressMutex"; // declare the mutext Mutex namedMutext; try { // test to see if the named mutex already exists namedMutext = isting(mutexName); } catch (WaitHandleCannotBeOpenedException) { // the mutext does not exist - we must create it namedMutext = new Mutex(false, mutexName); } // create the task Task task = new Task(() => { while (true) { // acquire the mutex ine("Waiting to acquire Mutex"); e(); ine("Acquired Mutex - press enter to release"); ne(); eMutex(); ine("Released Mutex"); } }); // start the task (); // wait for the task to complete (); } }}