|
Lock Challenge Step 4
Syncmultiplethreadssharingaresource
|
contains all functions to demo thread synch More...
#include <iostream>#include <thread>#include <mutex>#include <chrono>#include <condition_variable>#include <random>#include "LockGuard.h"
Go to the source code of this file.
Macros | |
| #define | NUM_THDS 3 |
Functions | |
| void | thd_printer (int id, std::string msg) |
| thread print function More... | |
| void | thd_worker (const int id, int &next_thd, std::default_random_engine &rand_e) |
| thread worker function More... | |
| int | main () |
Variables | |
| std::mutex | mtx |
| std::mutex | prnt_mtx |
| std::condition_variable | cond |
contains all functions to demo thread synch
Definition in file main_no_debug.cpp.
| #define NUM_THDS 3 |
Definition at line 25 of file main_no_debug.cpp.
| int main | ( | ) |
Definition at line 108 of file main_no_debug.cpp.
| void thd_printer | ( | int | id, |
| std::string | msg | ||
| ) |
thread print function
| id | an integer that defines thread number |
| msg | a string containing message to be printed |
Prints to standard out
Definition at line 44 of file main_no_debug.cpp.
| void thd_worker | ( | const int | id, |
| int & | next_thd, | ||
| std::default_random_engine & | rand_e | ||
| ) |
thread worker function
| id | an integer passed by val defining current thread number |
| next_thd | an integer passed by ref that keeps track of thread order |
| rand_e | a pseudo-random number generator engine passed by ref |
Upon startup, function blocks on condition_signal. Upon receipt of condition_signal, function sleeps at random for between 1 and 5 seconds, then signals next thread and goes back to blocking on condition_signal.
< restart sequence
Definition at line 65 of file main_no_debug.cpp.
| std::condition_variable cond |
Definition at line 31 of file main_no_debug.cpp.
| std::mutex mtx |
Definition at line 29 of file main_no_debug.cpp.
| std::mutex prnt_mtx |
Definition at line 30 of file main_no_debug.cpp.
1.8.13