10.5 Assignment

Overview

10.5 Assignment

Tasks and Locks

Work through the stories in order. Use the acceptance criteria to check each feature, and complete the nested tasks using the specified names, values, and scenarios.

Concurrency observations
Mechanism What to verify
Task scheduling Output order can vary between runs.
Wait The calling thread waits for completion.
lock One worker at a time enters the protected region.
SemaphoreSlim(2) At most two workers enter; acquisition order is not guaranteed.
Start concurrent work in three ways
Acceptance Criteria
Acceptance criteria
  • WriteToConsole accepts an output string and an iteration count and pauses 100 ms between iterations.
  • The three workers are started with a constructed Task plus Start, Task.Factory.StartNew, and Task.Run.
  • An unsynchronized run may interleave output; the specific order is not fixed.
Instructions
1.Create a new .NET Core console application
A Create a new .NET Core console application
2.In Main()
A Create a new static method named WriteToConsole
1 This method has 2 parameters, a string "output" and an int "numberOfIterations"
2 Create a for loop, using the numberOfIterations to set how many times the loop runs. Inside the loop, write the output to…

Create a for loop, using the numberOfIterations to set how many times the loop runs. Inside the loop, write the output to the console, then have the Thread.Sleep for 100 milliseconds.

B Create 3 tasks, using different methods to create them (new Task(), Task.Factory.StartNew() and Task.Run())
1 These tasks should all have an action that calls WriteToConsole, passing in a letter (a, b, or c) and 10 iterations.
2 Make sure to Start() the new Task().
3.Run the application - the output should be the previous letters in a random order.
A Run the application - the output should be the previous letters in a random order.
Wait for tasks to finish
Acceptance Criteria
Acceptance criteria
  • Main waits for a, b, and c as directed.
  • Waiting in that order does not require the workers to execute or print in that order.
Instructions
4.Adding Wait()
A By using Task.Wait(), we're able to ensure that a thread completes it's task before moving forward.
1 Use Task.Wait() to make the console output be all a's, then b's, then c's.
Control access to shared work
Acceptance Criteria
Acceptance criteria
  • One shared lock protects the entire repeated output operation; only one worker is inside that protected region at a time.
  • SemaphoreSlim(2) permits at most two workers into the protected operation concurrently.
  • All workers complete, and each acquired semaphore permit is released.
Instructions
5.Locks
A Instead of using Wait(), we could also create a lock for the WriteToConsole() method. By locking the method, we're able to…

Instead of using Wait(), we could also create a lock for the WriteToConsole() method. By locking the method, we're able to ensure that no new tasks are added that would break our queue for the outputs.

1 Create a new static object named consoleLock, and instantiate it at the field level.
2 Before the for loop in WriteToConsole, lock the method using the consoleLock.
B Run the application
1 You might get the output out of order - as in all b's, then c's, then a's. This is due to a race condition as per your…

You might get the output out of order - as in all b's, then c's, then a's. This is due to a race condition as per your previous readings. Each thread is racing to get to the lock, and whichever thread gets there first gets the lock.

C Using a SemaphoreSlim can allow us to let a certain number of threads in at a time, while managing a queue for the remaining threads.
1 Create a new field of type SemaphoreSlim named semaphore and instantiate this object at the field level, passing in 2 as an argument.
2 Remove the old lock, and replace it with semaphore.Wait(). This is the lock that checks if more threads are able to be…

Remove the old lock, and replace it with semaphore.Wait(). This is the lock that checks if more threads are able to be included in the semaphore.

3 After the for loop, add semaphore.Release(). This releases one of the locks, allowing a new thread to pass Wait().
D Run the application
1 Your output should be two characters alternating at random, with a third at the end printing out in order - like this…

Your output should be two characters alternating at random, with a third at the end printing out in order - like this "cacaacaccaacacacacacbbbbbbbbbb"

Completion review
6.Review the feature acceptance criteria
A Confirm the required results

Return to each story and verify its acceptance criteria. Preserve the submission format and destination stated in the activity; a reading, setup guide, lab, video demonstration, and oral final may require different evidence.

Start concurrent work in three ways Wait for tasks to finish Control access to shared work