Lock Challenge Step 4
Syncmultiplethreadssharingaresource
Macros | Functions | Variables
main.cpp File Reference

contains all functions to demo thread synch More...

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <condition_variable>
#include <random>
#include "LockGuard.h"
Include dependency graph for main.cpp:

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::mutex prnt_mtx
 
std::condition_variable cond
 
bool DEBUG = false
 

Detailed Description

contains all functions to demo thread synch

Note
set DEBUG to true in globals to turn on debug output file-wide, or uncomment it inside a function to enable function-specific debug output.
needs to be compiled with flag -pthread.
Author
Armaan Roshani

Definition in file main.cpp.

Macro Definition Documentation

◆ NUM_THDS

#define NUM_THDS   3

Definition at line 25 of file main.cpp.

Function Documentation

◆ debugPrinter() [1/3]

void debugPrinter ( int  id,
std::string  msg 
)

DEBUG PRINTER function.

Parameters
idan integer that defines thread number (0 for main)
msga string containing message to be printed

Prints to standard out

Warning
NOT thread-safe; must be called within a thread-safe scope

Definition at line 45 of file main.cpp.

◆ debugPrinter() [2/3]

void debugPrinter ( int  id,
int  next_thd 
)

Definition at line 59 of file main.cpp.

◆ debugPrinter() [3/3]

void debugPrinter ( int  id,
std::string  msg,
int  next_thd 
)

Definition at line 74 of file main.cpp.

◆ main()

int main ( )

create an array of NUM_THDS thread objects

create a pseudo-random engine

wait for 3 seconds

Definition at line 180 of file main.cpp.

◆ thd_printer()

void thd_printer ( int  id,
std::string  msg 
)

thread print function

Parameters
idan integer that defines thread number
msga string containing message to be printed

Prints to standard out

Warning
NOT thread-safe; must be called within a thread-safe scope

Definition at line 91 of file main.cpp.

◆ thd_worker()

void thd_worker ( const int  id,
int &  next_thd,
std::default_random_engine &  rand_e 
)

thread worker function

Parameters
idan integer passed by val defining current thread number
next_thdan integer passed by ref that keeps track of thread order
rand_ea 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.

Definition at line 120 of file main.cpp.

Variable Documentation

◆ cond

std::condition_variable cond

Definition at line 31 of file main.cpp.

◆ DEBUG

bool DEBUG = false

Definition at line 32 of file main.cpp.

◆ mtx

std::mutex mtx

Definition at line 29 of file main.cpp.

◆ prnt_mtx

std::mutex prnt_mtx

Definition at line 30 of file main.cpp.