views:

314

answers:

3

I'm not asking for anyone to do this homework for me, but I bring it up because it's a very good practical introduction to C# and threading, but at the same time I feel it's perhaps a little too simple.

Is this really the best way to teach threading? what key threading concepts are "lost" in this exercize, what would new programmers using threads for the first time likely fail to observe?

I have alot of theoretical knowledge about threading but haven't had to do alot of it myself in the past, does anyone have any caveats for me when writing it?

Here's the link to the original assignment: http://www2.cs.uidaho.edu/~sgotshall/cs204/CS_204_Assignment_5.pdf

and here is the goals text:

1) Create a thread-safe generic circular queue class and create a GUI to use it (See next section). In this context, thread safe means that each operation (method) that changes the contents of the queue should be executed by only one thread at a time in order to avoid data corruption. A circular queue is implemented as a fixed size array where the beginning and end of the queue are indices in the array. As the queue fills up, the beginning and end of the queue will shift to higher values as elements are added and eventually wrap around to the first index in the array to reuse the memory. This class should also throw an exception (specified below) to the caller if the operation is invalid.

2) Create a GUI to control two threads in a producer-consumer fashion. The GUI will be able to begin and start and stop both the producer and consumer threads and control the rate at which they modify the GenericCircularQueue.

+2  A: 

Read this:

The 2nd link can mostly stand on it's own.

Joel Coehoorn
good related reading, I really am shooting more for theoretical discussion here, but i appreciate the response!
Firoso
Thanks for bolding the text, it really gets my main point across better.
Firoso
+1  A: 

Read about SyncLock or Monitor assuming Windows C#.

It as good a way as any to understand multi-threading, especially in the day of multi-cores.

dbasnett
+1  A: 

I'd say that this misses two big things:

  • Lots of writes, reads can't get lock and starve; result: a bounded queue like this fills up. You need a way to give readers a higher priority so that they can drain the queue.
  • Scalability---you can make it thread-safe pretty easily by just requiring a lock to read or write, and that will work great for one reader/one writer. However, once you have large numbers of producers/consumers, you'll have a lot of thread contention.
Adam Jaskiewicz