Building Threading Samples
The Threading Samples need to be built before they can be run properly.
To
do so, please take the following single step:
· run nmake
Thread\CS_Sample\Simple.cs [Simple.exe]
This simple sample demonstrates the creation and running of a single thread.
The
output that is displayed simply says, "Alpha.Beta is running in
its own thread."
Thread\CS_Sample\StopJoin.cs [StopJoin.exe]
This simple sample demonstrates the creation, running, stopping, joining, and attempted restart of a single thread.
The output displayed says "Alpha.Beta is running in its own thread." several times.
Then,
it displays the line “Alpha.Beta has finished”
Finally, it will show that a stopped thread cannot be
restarted, and display two more lines of text before exiting.
Thread\CS_Sample\SyncOne.cs [SyncOne.exe]
This sample shows two threads writing to the console, without overwriting each other.
Try running this sample after having commented out the Monitor.Enter and Monitor.Exit lines and recompiling.
You will see the two
threads overwriting each other’s console output.
Thread\CS_Sample\SyncTwo.cs [SyncTwo.exe]
This sample shows two threads writing to the console while using
Monitor.Wait and Monitor.Pulse to keep from overwriting each other’s console
output.
Monitor\CS_Sample\MonitorSample.cs [MonitorSample.exe]
This sample demonstrates two threads, one a producer and the other a consumer, interacting with each other.
· Creates one empty storage slot that both the producer and consumer have access to.
·
Produces 20 items. It places each "produced item" in the storage slot whenever it is empty.·
Consumes 20 items. It gets a "produced item" from the storage slot whenever it finds there is one to consume.
ReaderWriterLock\CS_Sample\ReadWriteLock.cs [ReadWriteLock.exe]
This sample shows how ReaderWriterLock class prevents deadlocks when using multiple readers and multiple writers.
Four reader and four writer threads bounce through
WriterLock and ReaderLock states, without deadlocking, using the
ReaderWriterLock class.
Thread\CS_Sample\ThdRelStaticSample.cs [ThdRelStaticSample.exe]
This sample
shows how to use the attribute [ThreadStaticAttribute( )]
to make a static field act as a Thread Relative Static.
If you compile this sample with the "/D:WITHOUT"
switch you will see mainThd.thd1, mainThd.thd2, and mainThd all changing the
same field "E.stat" on each other.
ThreadPool\CS_Sample\SimplePool.cs [SimplePool.exe]
This sample demonstrates that queued user work items in a thread pool are run on several threads without the developer having to load balance the items in the code. The ThreadPool, and the Execution Engine, will load balance this at runtime.
1000 work items are queued up and serviced by the thread pool. Each work item records the id of the thread that serviced it. After all of the work items have been through the queue, the program displays a tally of how many items were serviced by each thread that was used in the thread pool.
Timer\CS_Sample\TimerSample.cs [TimerSample.exe]
This sample demonstrates the use of timers.
Items covered here are:
· Creating a TimerCallback, and using that TimerCallback to create new timer.
· Changing a timer's first fire time and the time
period used for each firing of the timer after the first
fire.
· Finally, disposing of a timer is covered.
WaitHandle\CS_Sample\MutexSample.cs [MutexSample.exe] This sample
demonstrates the use of Mutex, AutoResetEvent, and WaitHandle on
threads.
Four threads are created: · Thread 1 waits
on both a named mutex and an unnamed mutex. · Thread 2
waits on a named mutex.
· Thread 3
waits on either a named mutex or an unnamed
mutex. · Thread 4
waits on an unnamed mutex.