- Enure that the bool value for
DEBUG
inside of LockGuard.h is set totrue
. - In /src/Step_4/ directory run
make
. Executable will build as Lock.out. - To run executable and pipe standard error to a file, run
./Lock.out 2> error_log.txt
.
- LockGuard.h
- main.cpp (with DEBUG code)
- main_no_debug.cpp (identical, except without DEBUG code)
- error_log.txt
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 |
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.
- 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.
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.
Considerations for creating documentation remain the same as for Step 3: Reference in Step 3 Appendix
- 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
Environment remains the same as the previous steps.