CSC 420 Programming Assignment #3

"Multithreading Potpourri"


Description

Write a C++ program exploring multithreading and synchronization options in the PC WIN32 environment.

Your program will be a test bed for running three different experiments:

  1. Simpleton - Creating multiple threads without synchronization
  2. Smarty - Creating multiple threads and synchronizing their interaction using a mutex
  3. Bounded Buffer - Solving the bounded buffer problem using a mutex and semaphores

Program #3 is quite different from our first two programs. First, we will be using the Win32 API. I don't know how many of you are familiar with this protocol , so extensive code snippets and interface detail will be made available to you. 

My Test Bed

I have provided you with a C++ file to get you started. This file is located in the CSC 420 k: drive Common Area at:

Common Area/prog03/main.cpp

For you "at home" users, here's a link to that file:

main.cpp

I have commented this file, but if you still have questions, please email me at wikrieger@noctrl.edu 

The Help/Index menu in Visual C++ may also be very helpful to you.

I recommend, but do not require, that you simply copy this file and modify it. You should be able to complete Program #3 by modifying main.cpp

Simpleton Test

This is already done for you. All you need to do is run it and understand what is happening. Examples of some of the calls that you will need in the other tests are shown here.

You may notice that your output is jumbled. Why is this?

Smarty Test [25 pts]

The smarty test is very similar to the simpleton test, except that we will use a mutex to synchronize I/O.

This should fix your jumbled output problem. Comment on why.

Bounded Buffer Test [25 pts]

The Bounded Buffer problem is described in detail on page 206 of our text. Its solution requires both a mutex and two semaphores.

In my solution, I used the STL queue and stored integers. You can store whatever you like, and you may use whatever strucutre you like as well. I found the queue to be 

My output looks like this: out3.txt

Again, your output may differ depending on what data you store.

The Guts

We are using the Win32 API for Program #3. Here is the Win32 mapping for some of the concepts that you will use in Program #3:

Deliverables and Schedule

Program #3 is worth 50 points, or 5% of your class grade. It is due Thursday May 29. There will be no design step to program 3 because the design is straightforward and secondary to just exploring the properties of threads.

Please place your source code, executable, and output data in a folder called prog03 on your k: drive.

Deliverables include:

  1. Source code - commented and beautiful, of course, and this time, probably just one file.
  2. Executable  - I must be able to run your program and get your results
  3. Output files - for the Bounded Buffer test
  4. A README file - for organization notes or things I need to know when reviewing your program

Notes

I will further specify the Bounded Buffer experiments that I want you to run next week.

May 21, 2002 Note

Here are the Visual C++ web pages for the functions that you'll be using in Program #3. Please note that the links within these pages won't work... you can access them in Visual C++ Help/Index menu to get their full functionality:

Threads - CreateThread help page

I have an example call to this function in my main.cpp example. Note the use of the arrays to hold the handles and ID's of each thread as we create it. This array can then be used later as a parameter to the WaitForMultipleObjects() function, so that we can wait for all our threads to complete.

Mutexes - CreateMutex help page and ReleaseMutex help page

You will need to declare a Mutex variable and assign it with the return value of CraeteMutex(). In Win32's interface, WaitForSingleObject() is similar to a the wait() function for a mutex, and the ReleaseMutex() function is similar to signal().

Semaphores - CreateSemaphore help page and ReleaseSemaphore help page

The semaphore object is similar to the mutex object, except that the ReleaseSemaphore function (maps onto our signal() for a semaphore) has an additional parameter to increment the semaphore. It is common to increment the semaphore by 1 when you release it.

Wait functions - WaitForSingleObject help page and WaitForMultipleObjects help page

The wait functions are general purpose. They can be used to wait for a thread or threads to complete, or they can be used as the wait() function for a mutex or sempahore. Also note, that you can specify how long you want to "wait" before the function times out and returns. This can be set to the constant INFINITE (as in my main.cpp example), or it can be set to a value (in milliseconds). I used this feature to "timeout" my consumer thread after all the producers were done.

One hint on these calls, I used a lot of NULL parameters in my solution... our program is very simple, and we don't need a lot of the functionality provided in these calls.

May 23, 2002

In grading Program #3, first I will run your "Smarty" test and verify that it works correctly. Then, I'll also run a "Bounded Buffer" test on my own. Finally, please create the output files in your prog03 folder for the following Bounded Buffer tests:

File #

Producers

#

Buffers

Items per

Producer

Max

Wait

Notes
out01.txt 1 1 1 10 Min of all values
out02.txt 10 1 3 10 Mucho producers
out03.txt 10 20 3 10 Many producers, buffers
out04.txt 10 20 10 10 Mucho of everything
out05.txt 1 20 10 10 Few producers
out06.txt 3 5 10 0 Zero production time
out07.txt 3 5 10 100 Max production time

You will want to inspect each of these tests to verify for yourself that the output is what you expected and anomaly free... as I will.

As always, please include a README file containing your commentary for me to peruse and understand what you have done.