Click here to Skip to main content
12,747,719 members (40,469 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 9 Dec 2005

Demonstration of multi-threading with a distributed control

, 14 Dec 2005
Rate this:
Please Sign up or sign in to vote.
This is a small multi-threaded application that simulates cars moving in random directions.


This is a small multi-threaded application that simulates cars moving in random directions. Each car is moving independently from the other and is associated with a separate thread. A class CCarThread, derived from CWinthread, is written for this purpose. (The m_bAutoDelete parameter of CWinThread is set to false, so that the object survives even after the thread is terminated.)

A car is drawn using a bitmap resource and the parent window's device context. Since only one thread can own the device context at a time, a critical_section cs is used to synchronize the ownership.

The car can move in one of the following eight directions. The eight directions are assumed.

A separate bitmap for each direction is stored in the resources.

The random motion of the cars is obtained by the RandomVelocityChange() function which changes the velocity at a random time. In order to give this simulation a realistic look, the allowable change in the velocity is as follows:

where the solid black line shows the current direction of motion and the dotted black line shows the allowable directions it can change to, and the solid blue lines show the directions which are not allowed for the current direction to change to.

Collision detection

To detect the possibility of the collision of a car, each car thread checks itself against all the other cars. The thread pointer of all the cars are stored in a vector which is a static member of CCarThread. And any thread can access that vector using the critical_secion csCrash, which is also a static member of CCarThread.

The collision check is as follows: if the rectangular area occupied by this car overlaps that of another car then the two cars collide. (This is a type of distributed control.) It then signals the other car to terminate. Note that in order to avoid a deadlock, the cars which collide are moved to a separate array and are then killed.

In order to kill a thread, the event m_hEventKill of that thread is set, and then we wait for the thread to terminate itself. It then sends a message to the main window to remove or kill itself. You can remove all the cars any time during a simulation.


MSDN Articles


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Farhat Aisha
Web Developer
United Kingdom United Kingdom
I am a visual C++ programmer, currently working in Industrial Control Systems in Sheffield, United Kingdom as a Senior Programmer and Researcher. You can contact me by sending email to

You may also be interested in...


Comments and Discussions

QuestionCWinThread Pin
yfoulon14-Dec-05 7:23
memberyfoulon14-Dec-05 7:23 
Generalwrong category Pin
Dmitry Gaidamovitch13-Dec-05 0:08
memberDmitry Gaidamovitch13-Dec-05 0:08 
QuestionNice? Pin
Super Lloyd12-Dec-05 14:04
memberSuper Lloyd12-Dec-05 14:04 
AnswerRe: Nice? Pin
Farhat Aisha12-Dec-05 14:10
memberFarhat Aisha12-Dec-05 14:10 
GeneralRe: Nice? Pin
Super Lloyd12-Dec-05 14:18
memberSuper Lloyd12-Dec-05 14:18 
GeneralRe: Nice? Pin
Farhat Aisha12-Dec-05 14:33
memberFarhat Aisha12-Dec-05 14:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 14 Dec 2005
Article Copyright 2005 by Farhat Aisha
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid