Skip to content

Latest commit

 

History

History
157 lines (122 loc) · 4.43 KB

Step_4.md

File metadata and controls

157 lines (122 loc) · 4.43 KB

Step 4: Protect a Common Resource

To duplicate

  1. Enure that the bool value for DEBUG inside of LockGuard.h is set to true.
  2. In /src/Step_4/ directory run make. Executable will build as Lock.out.
  3. To run executable and pipe standard error to a file, run ./Lock.out 2> error_log.txt.

Files

Documentation

Doxygen documentation is available for this step of the project.

Step 4 main page
LockGuard.h Documentation Code
main.cpp Documentation Code
main_no_debug.cpp Documentation Code

Results

It produced this output to the terminal when combining standard out and standard error:

armaan@ubuntuVM:Step_4$ ./Lock.out
main: starting all threads
LockGuard adopted
thread3: starting, waiting.
LockGuard unlocked
LockGuard adopted
thread2: starting, waiting.
LockGuard unlocked
LockGuard adopted
thread1: starting, waiting.
LockGuard unlocked
LockGuard adopted
thread1: signal received, doing work ....
LockGuard unlocked
LockGuard adopted
thread1: done with work, signal next thread
LockGuard unlocked
LockGuard adopted
thread2: signal received, doing work ....
LockGuard unlocked
LockGuard adopted
thread2: done with work, signal next thread
LockGuard unlocked
LockGuard adopted
thread3: signal received, doing work ....
LockGuard unlocked
LockGuard adopted
thread3: done with work, signal next thread
LockGuard unlocked
LockGuard adopted
thread1: signal received, doing work ....
LockGuard unlocked
^C

And this output when only printing standard out:

armaan@ubuntuVM:Step_4$ ./Lock.out 2> error_log.txt
main: starting all threads
thread3: starting, waiting.
thread2: starting, waiting.
thread1: starting, waiting.
thread1: signal received, doing work ....
thread1: done with work, signal next thread
thread2: signal received, doing work ....
thread2: done with work, signal next thread
thread3: signal received, doing work ....
thread3: done with work, signal next thread
thread1: signal received, doing work ....
^C

And this output when only printing standard error:

armaan@ubuntuVM:Step_4$ cat error_log.txt
LockGuard adopted
LockGuard unlocked
LockGuard adopted
LockGuard unlocked
LockGuard adopted
LockGuard unlocked
LockGuard adopted
LockGuard unlocked
LockGuard adopted
LockGuard unlocked
LockGuard adopted
LockGuard unlocked
LockGuard adopted
LockGuard unlocked
LockGuard adopted
LockGuard unlocked
LockGuard adopted
LockGuard unlocked
LockGuard adopted
LockGuard unlocked
armaan@ubuntu373VM:Step_4$ 

These outputs match expectations.

Test conditions

  • Pass:
    • Duplicate behavior from Step 3.
    • Avoid anomalous thread behavior, including but not limted to, deadlock and data races.
    • Demonstrate that the resource is protected.
  • Fail: Any other result.

Method

Coding

I chose cout as my common resoure to protect. To accomplish this, I only need to lock a mutex and call my chal::LockGuard function from within the thd_printer again.

I debated about this for a bit. See Reference in Appendix for details. In the end, I decided to enable DEBUG printing inside of the chal::LockGuard class to demonstrate that locking and unlocking are occuring with each write to standard out from the threads.

Doxygen

Considerations for creating documentation remain the same as for Step 3: Reference in Step 3 Appendix

Requirements

  1. Use your implementation of c++11 lock_guard (NOT std::lock_guard) to protect a common resource in your threading example from #3 and #4

Prelims

Environment

Environment remains the same as the previous steps.