-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlight_controlled_intersection.py
33 lines (27 loc) · 1.74 KB
/
light_controlled_intersection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
There is a traffic light located on each road before the intersection. A traffic light can either be green or red.
Green means cars can cross the intersection in both directions of the road.
Red means cars in both directions cannot cross the intersection and must wait until the light turns green.
The traffic lights cannot be green on both roads at the same time. That means when the light is green on road A, it is red on road B and when the light is green on road B, it is red on road A.
Initially, the traffic light is green on road A and red on road B. When the light is green on one road, all cars can cross the intersection in both directions until the light becomes green on the other road. No two cars traveling on different roads should cross at the same time.
Design a deadlock-free traffic light controlled system at this intersection.
# -----------------------------------------------------------------------------------------------------------------
from threading import Semaphore
class TrafficLight:
def __init__(self):
# initialize a mutex
self.m = threading.Lock()
self.light = 1 # which light
pass
def carArrived(
self,
carId: int, # ID of the car
roadId: int, # ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
direction: int, # Direction of the car
turnGreen: 'Callable[[], None]', # Use turnGreen() to turn light to green on current road
crossCar: 'Callable[[], None]' # Use crossCar() to make car cross the intersection
) -> None:
with self.m:
if self.light != roadId:
self.light = roadId
turnGreen()
crossCar()