Many popular applications of multi-threaded programs are loosely coupled. Loosely coupled programs consist of multiple unrelated threads of execution. Loosely coupled multi-threaded applications are appealing because they allow the programmer to create each thread independently as if it were a separate program. After the programmer has coded and tested each thread, he/she easily integrates the threads to create a sophisticated program.Games are a common use of loosely coupled multi-threaded programs. Recently Steve Halladay built a take-off program on the old space invaders game using Multi-C. Those familiar with the game will remember that the game has armies, space ships, bombs, bullets and guns. Each of these objects operates independently of the other objects (unless a bomb or bullet contacts another object).
Steve created the game by building each object independently. The first object to be built was the space ship that flies across the top of the screen in random directions. The space ship was thoroughly tested independently of the other objects.
The next objects built were the gun and its bullets. The arrow keys and the space bar control the gun (i.e., the arrows move the gun right and left, and the space bar fires the gun). Loosely coupled multiple threads allowed the gun to be built independently. Integrating the gun thread with the space ship thread made it possible to test the ability to shoot down the space ship.
The loosely coupled threads allowed each additional object to be built, developed, and tested independently. The next step integrated the independent threads with the previously existing objects. The multi-threaded capabilities simplified the integration process considerably. Most of the integration was little more than linking in the new objects.
Once the game was complete, he wanted to create a demo that would play the game by itself. Demo development consisted of developing a thread to simulate keyboard activity and integrating the thread into the existing program. By using an additional thread to perform this function, it was simple and yet interesting to develop strategies for automating the game playing.
If you have further interest in loosely coupled multi-threaded programming or this example program, you can contact Steve Halladay for a copy of its source and executable. The program uses character graphics (via Aspen Scientific's version of curses) and requires less than 100K bytes to run. It uses an interrupt to continuously read the keyboard that may not be supported by some older versions of BIOS. The program and its source can be freely copied, but support is not available.