The final project for the Udacity C++ Nanodegree Concurrency course

Overview

CPPND: Program a Concurrent Traffic Simulation

This is the project for the fourth course in the Udacity C++ Nanodegree Program: Concurrency.

Throughout the Concurrency course, you have been developing a traffic simulation in which vehicles are moving along streets and are crossing intersections. However, with increasing traffic in the city, traffic lights are needed for road safety. Each intersection will therefore be equipped with a traffic light. In this project, you will build a suitable and thread-safe communication protocol between vehicles and intersections to complete the simulation. Use your knowledge of concurrent programming (such as mutexes, locks and message queues) to implement the traffic lights and integrate them properly in the code base.

Dependencies for Running Locally

Basic Build Instructions

  1. Clone this repo.
  2. Make a build directory in the top level directory: mkdir build && cd build
  3. Compile: cmake .. && make
  4. Run it: ./traffic_simulation.

Project Tasks

When the project is built initially, all traffic lights will be green. When you are finished with the project, your traffic simulation should run with red lights controlling traffic, just as in the .gif file above. See the classroom instruction and code comments for more details on each of these parts.

  • Task FP.1 : Define a class TrafficLight which is a child class of TrafficObject. The class shall have the public methods void waitForGreen() and void simulate() as well as TrafficLightPhase getCurrentPhase(), where TrafficLightPhase is an enum that can be either red or green. Also, add the private method void cycleThroughPhases(). Furthermore, there shall be the private member _currentPhase which can take red or green as its value.
  • Task FP.2 : Implement the function with an infinite loop that measures the time between two loop cycles and toggles the current phase of the traffic light between red and green and sends an update method to the message queue using move semantics. The cycle duration should be a random value between 4 and 6 seconds. Also, the while-loop should use std::this_thread::sleep_for to wait 1ms between two cycles. Finally, the private method cycleThroughPhases should be started in a thread when the public method simulate is called. To do this, use the thread queue in the base class.
  • Task FP.3 : Define a class MessageQueue which has the public methods send and receive. Send should take an rvalue reference of type TrafficLightPhase whereas receive should return this type. Also, the class should define an std::dequeue called _queue, which stores objects of type TrafficLightPhase. Finally, there should be an std::condition_variable as well as an std::mutex as private members.
  • Task FP.4 : Implement the method Send, which should use the mechanisms std::lock_guard<std::mutex> as well as _condition.notify_one() to add a new message to the queue and afterwards send a notification. Also, in class TrafficLight, create a private member of type MessageQueue for messages of type TrafficLightPhase and use it within the infinite loop to push each new TrafficLightPhase into it by calling send in conjunction with move semantics.
  • Task FP.5 : The method receive should use std::unique_lock<std::mutex> and _condition.wait() to wait for and receive new messages and pull them from the queue using move semantics. The received object should then be returned by the receive function. Then, add the implementation of the method waitForGreen, in which an infinite while-loop runs and repeatedly calls the receive function on the message queue. Once it receives TrafficLightPhase::green, the method returns.
  • Task FP.6 : In class Intersection, add a private member _trafficLight of type TrafficLight. In method Intersection::simulate(), start the simulation of _trafficLight. Then, in method Intersection::addVehicleToQueue, use the methods TrafficLight::getCurrentPhase and TrafficLight::waitForGreen to block the execution until the traffic light turns green.
You might also like...
This is a project from my Embedded Systems Lab course. It's made using C and intended for the MSP430FG461 microcontroller.

This is a project from my Embedded Systems Lab course. It's made using C and intended for the MSP430FG461 microcontroller.

Virtua Fighter 5: Final Showdown Unlocker for Yakuza 6 and Yakuza: Like a Dragon
Virtua Fighter 5: Final Showdown Unlocker for Yakuza 6 and Yakuza: Like a Dragon

Virtua Fighter 5: Final Showdown Unlocker This plugin for Yakuza 6 and Yakuza: Like a Dragon modifies in-game Virtua Fighter 5: Final Showdown arcades

Trabajo final de la asignatura de Sistemas Operativos de Ingeniería de Telecomunicaciones. Leer la memoria para más información.

# Domotica Trabajo final de la asignatura de Sistemas Operativos de Ingeniería de Telecomunicaciones. Leer la memoria para más información. Este proy

Practice questions for midterm & final (no solutions)

Comp 15 Tufts Reviews Here are some reviews for the COMP15 midterm and final. These are not the official course study guides, but rather written by me

Is this hardware solution the ultimate in switch bounce elimination? Simple PCB, cheap components = final solution!
Is this hardware solution the ultimate in switch bounce elimination? Simple PCB, cheap components = final solution!

#230 Ultimate Switch Bounce Eliminator Direct link to video: https://youtu.be/b2uUYiGrS5Y It's time to try a final, alternative approach to the ubiqui

Final Assignment for Embedded Real Time Operating Systems at UCSD Extension.

Final Assignment for Embedded Real Time Operating Systems at UCSD Extension. This program is for a certificate in Embedded Software Engineering at UCSD. We used FreeRTOS running on a STM32L475G Microcontroller.

An open source re-implementation of Final Alert 1.0.2 written in C++.

Motivation The project is a rewrite of Final Alert 2, a map editor for Command and Conquer: Red Alert 2 and Command and Conquer: Yuri's Revenger and t

An in-progress matching decompilation of Final Fantasy VII For the PSX.

FFVII An in-progress decompilation of the original US release of Final Fantasy VII on the PSX. Building (Linux) Install build dependencies The build p

Problem set of the final contest from CTF COMPFEST 13

CTF COMPFEST 13 - Final This repository contains the problem set of final contest from CTF COMPFEST 13. Table of Contents Problem Title Author Categor

Owner
Franz
https://fjp.at/about/
Franz
This is a project for Rowans ECE Final Project

ECEExposureProject This is a project for Rowans ECE Final Project There are 3 files that can be found in this repository. DHT PIP-Brick - This program

Craig Droke 1 Dec 8, 2021
Team 3's final project for ECE 485/585 F'21

ECE585_Team3 Team 3's final project for ECE 485/585 F'21 Braden Harwood, Stephen Short, Michael Weston, Drew Seidel *Information sourced from Dr. Faus

null 1 Dec 10, 2021
Final project for the Programming I subject of the Computer Engineering Degree.

2048 Final project for the Programming I subject of the Computer Engineering Degree. 1. Introduction 2048 is a puzzle game for one player. Its main go

André Rato 1 Oct 28, 2021
Final version of my dissertation project at the University of Birmingham as part of MSc. Computer Science degree.

Unfair Edge: A Low-Level Manipulation of Game Memory with Bypassing VAC This repository hosts the code submitted as a dissertation project for MSc. Co

70xhandler 1 Nov 4, 2021
Undergrad Final Project

Smart-Water-Management-System Final Project 2021 Abstract The Smart Water Management System project aggregates three meters to enable remote and autom

Chrispine Tinega 9 Jul 5, 2022
Flutter TODO application for cs50 final project with hive database.

TODO application TODO apllication for CS50 final project with flutter framework. Video Demo: https://youtu.be/91qB2TgGXTc Description: This is my Fina

Seyyed Mohammad Hossein Nabavi 0 Jul 25, 2022
Use this to speed up your final project and reduce code bloat

224 Superior Serial.print statements Use this to speed up your final project and reduce code bloat! And we learn about printing formatted strings usin

Ralph Bacon 28 Jan 4, 2023
[ICSE 2022] Controlled Concurrency Testing via Periodical Scheduling

(PERIOD is a controlled concurrency testing tool, where a scheduler explores the space of possible interleavings of a concurrent program looking for b

Cheng Wen 21 Dec 7, 2022
this is all about the EE222 course project

Embedded_System course project (Folder) 嵌入式 contains all details about EE222 include some code in the project and some homework in this class. (Folder

null 2 Oct 1, 2022
Undergrad Course CS252 Project Solutions

191CV210_CS252_PROJECT Details/Documentation for individual project constituting of the following problem statements from the 10th edition of Silbersc

Chaitanya Shashwat 1 Nov 21, 2021