Lock Challenge Step 4
Syncmultiplethreadssharingaresource
Macros | Functions | Variables
main_no_debug.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_no_debug.cpp:

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
 

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
Bug:
no known bugs

Definition in file main_no_debug.cpp.

Macro Definition Documentation

◆ NUM_THDS

#define NUM_THDS   3

Definition at line 25 of file main_no_debug.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 108 of file main_no_debug.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 44 of file main_no_debug.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.

< restart sequence

Definition at line 65 of file main_no_debug.cpp.

Variable Documentation

◆ cond

std::condition_variable cond

Definition at line 31 of file main_no_debug.cpp.

◆ mtx

std::mutex mtx

Definition at line 29 of file main_no_debug.cpp.

◆ prnt_mtx

std::mutex prnt_mtx

Definition at line 30 of file main_no_debug.cpp.