Concurrent-Traffic-Simulation

所属分类:硬件设计
开发工具:C++
文件大小:0KB
下载次数:0
上传日期:2019-09-08 15:05:12
上 传 者sh-1993
说明:  c++并发编程,
(c++ concurrent programming,)

文件列表:
CMakeLists.txt (523, 2019-09-08)
data/ (0, 2019-09-08)
data/nyc.jpg (3722720, 2019-09-08)
data/paris.jpg (11903059, 2019-09-08)
data/traffic_simulation.gif (2049146, 2019-09-08)
src/ (0, 2019-09-08)
src/Graphics.cpp (2553, 2019-09-08)
src/Graphics.h (766, 2019-09-08)
src/Intersection.cpp (4417, 2019-09-08)
src/Intersection.h (1855, 2019-09-08)
src/Street.cpp (573, 2019-09-08)
src/Street.h (1003, 2019-09-08)
src/TrafficLight.cpp (2557, 2019-09-08)
src/TrafficLight.h (920, 2019-09-08)
src/TrafficObject.cpp (636, 2019-09-08)
src/TrafficObject.h (1075, 2019-09-08)
src/TrafficSimulator-Final.cpp (5631, 2019-09-08)
src/Vehicle.cpp (5027, 2019-09-08)
src/Vehicle.h (1063, 2019-09-08)

# CPPND: Program a Concurrent Traffic Simulation This is the project for the fourth course in the [Udacity C++ Nanodegree Program](https://www.udacity.com/course/c-plus-plus-nanodegree--nd213): 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 * cmake >= 2.8 * All OSes: [click here for installation instructions](https://cmake.org/install/) * 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](https://developer.apple.com/xcode/features/) * Windows: [Click here for installation instructions](http://gnuwin32.sourceforge.net/packages/make.htm) * OpenCV >= 4.1 * The OpenCV 4.1.0 source code can be found [here](https://github.com/opencv/opencv/tree/4.1.0) * 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](https://developer.apple.com/xcode/features/) * Windows: recommend using [MinGW](http://www.mingw.org/) ## 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` 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` 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.

近期下载者

相关文件


收藏者