CSS 443 -
Advanced Programming Methodologies

Kelvin Sung (ksung@u.washington.edu)
Spring 2003

 

CSS443        Computing and Software Systems        UW Bothell   

 

Examples on Threads

Here are the sample source codes on C++/Win32 threads we discussed in lectures.

Here are some of the MSDN VC6.0 on: win32 thread programming and on using the synchronization objects.

Explanation

Source File

(source only, must download/compile to run)

 

 

TthreadBase – Base for creation of new worker threads. Here we see how _beginThread() can be used. The actual work is performed in StupicThreadWorker (Yes, I can’t spell, what other problems do you have? J) simulates working by sleeping.

 

To See what is going on:

  1. TaskManager: left-mouse-button, switch on “Thread” column and see there are 6 threads running for the “test” process, and as each thread quits, you can see this number decrease.
  2. We can predict when each thread will quit (look at the source, Thread0-takes 5x6=30 seconds to quit, Thread-1 takes5x5=25 seconds to quit.), so by looking at the wall clock, and the quit message we can see the threads are actually running simultaneously. Cool eh?

source

for download

 

 

Same TthreadBase with added functionality of fShouldTerminate. This allows external control of determining if a thread should terminate. This time, we retain main’s reference to the created threads (workThreads[] array), and let user decide which thread should terminate.

 

Notice, if we terminate main before the workThreads are properly terminated (with _endThread()), all children threads will terminate immediately! Results/Data from the worker thread may be lost! Main should never  terminate without making sure all children threads are done with their work!

source

for download

 

 

Simulate Priority: e.g. all student threads are running, but as soon as they notice Kelvin thread starts, student threads should stop and wait until Kelvin thread is done. How can we achieve this?

 

We will use CEvent object to help us achieve this. Relevant functions for CEvent: Set/Unset/Lock.

 

Notice, KelvinThread that chances are, when KelvinThread first started, studentThreads are running also. When studentThread tries to get another job, they get blocked, and have to wait for KelvinThread finish before they can do another job.

 

Another function implemented is, the TsyncObject accumulating TotalWorkTime, in this case we have one variable updated by multiple studentThreads, and we must protect the access of this variable. This is achieved by CMutex, to ensure only one thread has access to the variable. Notice in the code we simulate += with explicit variable transferring and sleep() to simulate delay. Try commend out the Mutex protection to see wrong answer resulting in total accumulated work time.

source

for download

 

 

Worker ID Thread: Finally something pseudo real: here we have a system where we let the user choose which workThread gets to work on each job. If a work is busy doing work, the main thread will be blocked, otherwise, after assigning the work, the user can continue to the next job assignment. Now how can we do this?

 

We get an id from the user and pass this id to all threads, the thread with the matching id will erase the command, and go do the work. Besides protecting the id (with CMutex) to make sure no two threads can change this at the same time, we have two other synchronization tasks:

1.        All Worker Thread: should wait for command coming from the main thread, and should not do anything while waiting. We will use a CEvent (fNextCommandEvent) to achieve this. Initially, there is no “NextCommandEvent”, so all workers will wait (stop at red-light).

2.        Next Command: after the main thread got a command, it has to make sure the previous command has been read by the corresponding worker thread. Here we will use another CEvent (fCommandReadEvent) to synchronize our threads. Main thread resets this event, and the corresponding thread will set this event.

source

for download

 

 

Print Thread: Lastly something relevant to our mp2, here is using what we have learned from above to implement a simple printing thread (based on my mp1 solution). No synchronization is attempted in this example. This means, it is very easy to create undefined output. For example, insert 1,2,3,4,5 and issue print command (started the print thread), now very quickly, delete 2 from the linklist. Now what should the output of the print command be (with or without the 2)? You see, it is important, that while a thread is reading a list (to print it), no other thread should be allowed to change the list (delete).

Source

for download

 

 

 

 

 

 

  CSS 443 Home  |   Kelvin Sung  |   CSS Home  |   UW Bothell  |   UW Seattle


UWB Home
18115 Campus Way NE
Bothell, WA 98011-8246
(425) 352-5000
(425) 352-5303 (TDD)

*


University of Washington, Bothell
Copyright ©2003, UWB. All rights reserved.
Comments to
Kelvin Sung: ksung@u.washington.edu
Information about UWB: uwbothel@u.washington.edu

Last updated: January 2003 KS