This project practices C++ concurrency.
I found this resource I really liked which introduced multithreading and concurrency, especially because this line -
Multithreading often carries a reputation for being difficult. Compared with other concepts in software development, one could certainly make a case for that. However, multithreading isn’t really that different from general programming. It’s just potentially more dangerous . Learning to protect against the danger, though, can allow one to implement far more powerful algorithms and programs than you could in a single threaded manner."
Or, more or less, the data races are not 100% guaranteed to show up per run. You should be able to spot them on your own.
I was very interested in the fact that, because threads do not have a copy constructor, a vector of threads needs to be populated by the move constructor and therefore by using emplace_back
; also, when multiple threads are running, interleaved work can present a bug such as interrupted print statements in cout
- cout
is a shared resource that can be accessed in the middle of executing the same method by another thread (??).
Notable code changes made in the project:
- The basic structure of the project was already implemented- vehicles heading to destinations, communicating with an open intersection, and passing through one at a time. What was implemented was the ability for a traffic light to have a red light, and therefore pause the flow of cars. In addition to FIFO of cars being let through, the lights cycle between red and green. Every
Intersection
owns aTrafficLight
that owns a thread that runs an infinite loop sending color messages to a message queue also owned by and wrapped byTrafficLight
. - Fleshed out a generic message queue with implementation split across
TrafficLight.h
andTrafficLight.cpp
. Added some connecting code inIntersection
, with a notable lines beingL87
toL91
. Every method inTrafficLight.cpp
is newly implemented. - This wasn't in the project spec at all, but I wanted to pass in a parameter to the driver that would allow CLI toggling
between the two cities Paris and NYC. The data and functions were already pre-written within the driver, so all it needed was a user argument.
I changed the
int main()
function inTrafficSimulator-Final.cpp
toint main(int argc, char** argv)
and used the string to toggle, with the default/for malformed input being Paris. Had to change a C-style string to anstd::string
.
- cmake >= 2.8
- All OSes: click here for installation instructions
- make >= 4.1 (Linux, Mac), 3.81 (Windows)
- Linux: make is installed by default on most Linux distros
- Mac: install Xcode command line tools to get make
- Windows: Click here for installation instructions
- OpenCV >= 4.1
- The OpenCV 4.1.0 source code can be found here
- gcc/g++ >= 5.4
- Linux: gcc / g++ is installed by default on most Linux distros
- Mac: same deal as make - install Xcode command line tools
- Windows: recommend using MinGW
Two Options -
- Only download the executable
traffic_simulation
from thebuild
folder, and run it through the./traffic_simulation
command in the same directory.
Or-
- Clone repo to local.
- Make a build directory in the top level directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it through the following command:
./traffic_simulation
.
To run with Paris as the background, either ./traffic_simulation
or ./traffic_simulation paris
.
To run with NYC as the background, ./traffic_simulation nyc
.