0% found this document useful (0 votes)
67 views

Concurrent Traffic Simulation

The final phase of the traffic simulation project introduced traffic lights to intersections to improve road safety with increasing traffic. A new TrafficLight class was added to organize traffic flow, with intersections switching periodically between red and green states. Vehicles can pass during green lights but not red lights. Message queues were implemented for communication between vehicles and intersections. Intersections randomly change state every 4-6 seconds through the simulate function to transition lights from red to green. This phase completed the multi-threaded traffic simulation with organized traffic flow controlled by traffic lights.

Uploaded by

Abdelrhman Tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Concurrent Traffic Simulation

The final phase of the traffic simulation project introduced traffic lights to intersections to improve road safety with increasing traffic. A new TrafficLight class was added to organize traffic flow, with intersections switching periodically between red and green states. Vehicles can pass during green lights but not red lights. Message queues were implemented for communication between vehicles and intersections. Intersections randomly change state every 4-6 seconds through the simulate function to transition lights from red to green. This phase completed the multi-threaded traffic simulation with organized traffic flow controlled by traffic lights.

Uploaded by

Abdelrhman Tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

CONCURRENT

TRAFFIC
SIMULATION
Multi Threading
CONTENTS
1-Overview
2-Phases overview
3-Phase 1
4-Phase 2
5-Phase 3
6-Final Phase
OVERVIEW
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, i build a
suitable and thread-safe communication protocol between vehicles
and intersections to complete the simulation. Use The knowledge of
concurrent programming (such as mutexes, locks and message
queues) to implement the traffic lights and integrate them properly in
the code base.
PHASES
OVERVIEW
Every Phase Either Add New Features Or Fixes Problems in the Previous Phase

Data Traff
Base Safe
Race ic

Phase 1 Phase 2 Phase 3 Final phase


Put in The Fundamental Base Communications Between Add Locks Organized Traffic System
Describes The Purpose of every Vehicles And Intersections
class To increase Safety
PHASE 1
Main Flow
of Phase 1
PROGRAM STRUCTURE
PHASE 1 SUMMARY

TrafficObject(Parent Class)
- I f i rst c re ate d A e n u m t h a t d e s c r i b e s a l l p o s s i b i l i t i e s o f o b j e c t t y p e ( N o O b j e c t , Ve h i c l e ,

I n te rs e c t i o n , S t re e t )

- A n d C re ate d T h re e Att r i b u te s T h a t d e s c r i b e s T h e O b j e c t ( Ty p e o f O b j e c t , I D , Po s i t i o n ( x & y ) ,

Ve c to r o f t h re a d s to co nta i n a l l t h re a d s t h at h av e b e e n l a u n c h e d b y t h i s T h re a d )

- A n d I a d d a d d i t i o n a l a tt r i b u te ( i d c n t ) , To co u n t i d o f o b j e c t S o e ve r y o b j e c t h a s i t s u n i q u e I D

- A n d At t h e E n d I c re a te d t h e d e st r u c to r To m a ke a t h re a d B a r r i e r To m a ke s u re t h at a l l t h e t h re a d s

J o i n e d s u c c e s sf u l l y .
Intersection(Child Class)
- I f i rst c re ate d a Att r i b u te N a m e ( _ S t re e t s ) to D e te c t a l l t h e st re e t s co n n e c t e d to t h i s i n te rs e c t i o n

- T h e n I c re ate C o n st r u c to r To D ef i n e T h e o b j e c t t y p e ( i n te rs e c t i o n Ty p e ) , A n d t wo m e t h o d s

1 - A d d S t re e t : To a d d st re e t to v e c to r _ S t re e t s

2 - q u e r y S t re e t s : To re t u r n t h e ( o u t g o i n g ) S t re e t s
Vehicle(Child Class)
- c re ate C o n st r u c to r To D ef i n e T h e o b j e c t t y p e ( Ve h i c l e Ty p e )

- M e t h o d S e t C u r re n t S t re e t / D e st i n a t i o n ( ) : To d e te r m i n e T h e c u r re n t S t re e t A n d D e st i n at i o n

- M e t h o d S i m u l a te ( ) : I t ’s t h e M e t h o d W h i c h i s re s p o n s i p l e To s e n d T h e t h re a d s To D r i ve M e t h o d

- M e t h o d D r i v e ( ) : I t ’s t h e M a i n M e t h o d h e re A n d T h e M a j o r i t y o f t h e wo r k i s d o n e h e re W h i c h D o :

1 - i n i t i a l i ze S to p Wa tc h 4 - S l o w D o w n S p e e d w h e n w h e n we re a c h 9 0 % o f d e st i n a t i o n

2 - e n te r i n f i n i te L o o p 5 - i f we re a c h e d t h e d e st i n at i o n we w i l l ra n d o m i ze t h e n ex t st re e t

3 - U p d ate Po s i t i o n 6 - Re s e t T h e s p e e d A n d re s e t S to p Watc h
PHASE 2
Main Flow Of Phase 2
THE NEW FEATURES
THAT THE SECOND
PHASE ADDED

We s o l ve d a d a n ge ro u s p ro b l e m w e fa c e d i n t h e p r e v i o u s P h a s e a n d i t s t h at ( M o r e t h a n o n e c a r c a n

e nte r t h e i nte rs e c t i o n i n t h e s a m e T i m e , S o i n t h i s P h a s e I a d d e d n e w fe at u r e ( Ve h i c l e s Wa i t i n g

Q u e u e ) , A n d p e r m i tte d to O n l y o n e c a r to E nte r t h e i nte rs e c t i o n By c r e at i n g n e w F u n c t i o n

i nte rs e c t i o n S i m u l ate To s e n d A t h r e a d t h at p ro c e s s T h at q u e u e A n d i t w i l l b e d i s c u s s e d i n t h e n ex t

c o u p l e o f p a ge s .
FILES
MODIFICATIONS

1-Traffi cO b j e ct : T h e r e i s n o m o d i f i c at i o n s S i n c e I d i d n o t a d d a ny N e w O b j e c t

2-Vehicle : W h e n T h e ve h i c l e A b o u t to e nte r T h e i nte rs e c t i o n I a d d e d n e w Fe at u r e , I m a ke A ta s k

To a d d Ve h i c l e To Q u e u e F u n c t i o n A n d Wa i te d to m a ke S u r e t h e ve h i c l e c a n e nte r t h e i nte rs e c t i o n

3-Stre et : T h e r e i s n o m o d i f i c at i o n s

4-inte rse ctio n : ( M a i n m o d i f i c at i o n s i s d o n e h e r e )

F i rst I a d d e d n e w C l a s s ( Wa i t i n g Ve h i c l e s ) A n d i t r e p r e s e nt s T h e Q u e u e W h i c h H a s :
- Tw o Att r i b u te s :

- ( _ Ve h i c l e s ) W h i c h re p re s e n t s t h e Wa i t i n g C a r to e n te r t h e Q u e u e

- ( _ P ro m i s e s ) W h i c h re p re s e n t s l i s t of a s s o c i a t e d p ro m i s e s

- Three Method :

- ( ge t S i ze ( ) ) W h i c h Re t u r n t h e N u m b e r o f Ve h i c l e s i n t h e q u e u e

- ( p e r m i t E n t r y To F i r s t I n Q u e u e ( ) ) W h i c h Pe r m i t t h e f i r s t Ve h i c l e To e n t e r i n t e r s e c t i o n

- ( p u s h ba c k ( ) ) W h i c h A d d n e w Ve h i c l e To t h e e n d of t h e Q u e u e
And in The intersection Class I added :

- N e w At t r i b u t e ( i s B l o c ke d ) : W h i c h re p re s e n t T h e i n t e r s e c t i o n i s b l o c ke d O r n o t

- New Four Methods :

- s e t I s B l o c ke d ( ) : To c h a n g e t h e St a t of t h e i n t e r s e c t i o n ( B LO C K E D / F R E E )

- a d d Ve h i c l e To Q u e u e ( ) : To P u s h B a c k T h e Ve h i c l e i n Q u e u e A n d i t s a s s o c i a t e d p ro m i s e

- s i m u l a t e ( ) : v i r t u a l f u n c t i o n w h i c h i s e x e c u t e d i n a t h re a d

- ve h i c l e H a s L e f t ( ) : U s e d To C o n v e r t T h e St a t of i n t e r s e c t i o n t o F R E E
PHASE 3
THE NEW FEATURES
THAT THE SECOND
PHASE ADDED

At t h i s Po i nt T h e Tra f f i c S i m u l at i o n S e e m s To b e Wo r k i n g N i c e l y , H o w e ve r , T h e r e I s a d ata ra c e

h i d d e n i n t h e c o d e : T h e A c c e s s to t h e s h a r e d Ve c to rs ( _ Ve h i c l e s ) A n d ( _ P ro m i s e s ) i n c l a s s

Wa i t i n g Ve h i c l e s i s n o t p ro te c te d , t h i s b u g w i l l b e c o r r e c te d w i t h t h i s P h a s e . A l s o , A c c e s s to t h e

c o n s o l e s h a l l b e p ro te c te d s o t h at P r i nte d S t r i n g s a r e N o t m i xe d u p . S o w e w i l l S e e T h e m o d i f i c at i o n

To t h e F i l e s i n t h e n ex t c o u p l e Pa ge s .
F i rst i n ( Wa i t i n g Ve h i c l e s C l a s s ) i a p p l i e d L o c k i n g M e c h a n i s m to P r i vate m e m b e rs ( _ Ve h i c l e s &

_ P ro m i s e s ) To Avo i d D e a d l o c ks .

T h e n i C r e ate d n e w S tat i c M u tex ( c a l l e d _ mt xc o u t ) To u s e i t i n t h e n ex t S te p .

F i n a l l y i n ( a d d Ve h i c l e To Q u e u e ) I e n s u r e d t h at t h e tex t o u p u t l o c ks t h e c o n s o l e a s a s h a r e d r e s o u rc e

, U s i n g T h e m u tex ( _ mt xc o u t )
FINAL
PHASE
THE NEW FEATURES
THAT THE SECOND
PHASE ADDED

T h e Tra f f i c s i m u l at i o n i n i t s c u r r e nt s tate i s w o r k i n g f i n e A n d ve h i c l e s a r e m o v i n g a l o n g st r e e t s a n d

a r e c ro s s i n g i nte rs e c t i o n s . H o w e ve r, w i t h i n c r e a s i n g t ra f f i c i n t h e c i t y , t ra f f i c l i g ht s a r e n e e d e d fo r

ro a d u s e r s a fe t y . Ea c h i nte rs e c t i o n w i l l t h e r e fo r e b e e q u i p p e d w i t h t ra f f i c l i g ht . I n t h e p ro j e c t , a

s u i ta b l e a n d t h r e a d - s a fe c o m m u n i c at i o n p ro to c o l b e t w e e n ve h i c l e s , i nte rs e c t i o n s a n d t ra f f i c l i g ht s

n e e d s to b e e s ta b l i s h e d .
MAIN FLOW OF
FINAL PHASE
NEW CLASS(TRAFFIC
LIGHT )

A d d e d N ew c l a s s Traf f i c L i g ht , To o rga n i ze T h e t raf f i c , T h e i nte rs e c t i o n sw i tc h e s b et we e n R E D a n d

G R E E N , G re e n m e a n s ve h i c l e ca n p a s s , Re d m e a n s t h e ve h i c l e ca nt p a s s , A n d I c re ate d M e s s a ge

Q u e u e to co m m u n i cate b et we e n ve h i c l e s , A n d I ra n d o m T h e i nte rs e c t i o n eve r y 4 to 6 s e co n d s i t

c h a n ge s i t s state f ro m re d to g re e n . T h ro u g h t h e s i m u l ate f u n c t i o n .
THANK YOU
Abdelrahman Tarek Mahmoud
Code :
https://github.com/AbdelrahmanTarekMahmoud/C
ppND-Program-a-Concurrent-Traffic-Simulation
Youtube :
https://www.youtube.com/watch?v=RkE2Qnw3Bk
U

You might also like