CSC 216/s08/but one lamp

From Expertiza_Wiki
Jump to navigation Jump to search

Formatting Resources

Formatting Help Guide from MetaWiki

An Introduction to Threads

This project will introduce threads to students who have no previous knowledge on the subject.

What are Threads?

Threads are a way to break up your machine's processing so it can do many tasks all at the same time. These tasks are able to share variables, monitor each other’s progress, and begin and terminate separately.

Participants and props

For this too work, six students with a healthy desire for competition must be chosen from the class.

The only required materials are two decks of playing cards, eight note cards, and a table to sit around.

How it works

Table Setup


Each team should have four square "variables" or note cards, that represent either an Array or Vector.(Depending on previous knowledge and/or whether or not array resizing should be incorporated into the exercise) These Vectors are normal instance variables for the single student team. For the 5 student team, the Vectors are instance variables of the Driver and therefore shared variables amongs the threads.

Game Summary

First the teacher should get out two decks of cards, remove the jokers, and shuffle well. He should then get a single volunteer from the class and hand him one of the decks. Next, the teacher should find five more students ready to compete against the other student giving one of the other students the second deck. The teacher will instruct the single student that when he says go, that student should sort the card into suite and then order each suite from 2 to Ace. He will then instruct the group of five that when he says go, the student with the deck, the Driver, should count out exactly thirteen cards and give it to one of his fellow students, a thread. He should repeat until all the cards are given out. As soon as a thread gets a deck it may begin to sort it into each suite by placing a card in it's coresponding vector. A thread may not pick up any cards unless handed it by the driver. It may, however, begin it's task as soon as it receives cards without having to wait on another thread to be initialized. Once all threads have terminated the suite sort, the driver must then give each thread a stack (suite) to sort from 2 to Ace. The group of five people should finish much faster than the single person.

This simulation should show that by breaking up processing in this way, threads can accomplish a task faster than a single thread. The teacher should then go on to teach some of the more intricacies of threads.