Lock Challenge Step 3
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 | debugPrinter (int id, std::string msg) |
DEBUG PRINTER function. More... | |
void | debugPrinter (int id, int next_thd) |
void | debugPrinter (int id, std::string msg, int next_thd) |
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::condition_variable | cond |
bool | DEBUG = false |
contains all functions to demo thread synch
Definition in file main.cpp.
void debugPrinter | ( | int | id, |
std::string | msg | ||
) |
int main | ( | ) |
void thd_printer | ( | int | id, |
std::string | msg | ||
) |
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.
< time to randomize