|
|
Title |
Lets get threading.
|
Summary |
This simple code segement shows how to create new thread of execution and how to sychronise a section of the shared code. |
Contributor |
John McTainsh
|
Published |
14-Jun-2001 |
Last updated |
14-Jun-2001 |
|
|
Description
This code sample will create a working thread and demonstrate locking a
small section of the thread. This is by no means a who is what of threading. It
should just get you running (Ha ha.). Later parts to the threading saga will
should cover detailed thread issues such as synchronisation and priority.
Includes
To create a thread we need to include the following namespace.
using System.Threading; // Thread information
Here is the code
The following class will be created and the OnThreadStart will
be the starting point of the new thread. It looks like any other class except
the OnThreadStart function must have the declaration of public void
Xxxxxx() to suit the delegate. Once running the object will
print the thread name and a count to 100. Note: The lock ensures
only one thread of execution enters the lock area for each instance of MyThreadRunner .
/// <summary>
/// This class is the object created to be run by the thread new
/// threads. It is created and the Start Delegate is called to run
/// a thread through the OnThreadStart
/// </summary>
class MyThreadRunner
{
/// <summary>
/// A delegate is created for this function and passed to a
/// Thread object to execute.
/// </summary>
public void OnThreadStart()
{
// Prevent two threads entering this thread for 'this' object
lock( this )
{
for( int n = 0; n < 100; n++ )
{
Console.Out.WriteLine( "OnThreadStart(), Name {0}, {1}",
Thread.CurrentThread.Name, n);
}
}
}
}
The following code will launch two new threads as follows;
- Two objects are created, each will be launching a new thread. (To begin
with).
- The two threads are created using the
ThreadStart delegate.
- The threads are named to make display clearer.
- The threads are run and left to there own devices.
// Create the object to house the thread
MyThreadRunner myRunner1 = new MyThreadRunner();
MyThreadRunner myRunner2 = new MyThreadRunner();
// Create the thread, passing a delegate to start at.
Thread myThread1 = new Thread( new ThreadStart( myRunner1.OnThreadStart ) );
Thread myThread2 = new Thread( new ThreadStart( myRunner2.OnThreadStart ) );
//Name the thread
myThread1.Name = "Daisy";
myThread2.Name = "X1";
// Start the threads executing
myThread1.Start();
myThread2.Start();
When the above code executes, depending on your CPU configuration and a
zillion other factors, the output text would be intermingled every couple of
lines. This is to be expected. To make only one thread enter the lock section at
any instant we must only create one myRunner object and pass the
same object to both myThread1 and myThread2 as
follows.
// Create the object to house the thread
MyThreadRunner myRunner = new MyThreadRunner();
// Create the thread, passing a delegate to start at.
Thread myThread1 = new Thread( new ThreadStart( myRunner.OnThreadStart ) );
Thread myThread2 = new Thread( new ThreadStart( myRunner.OnThreadStart ) );
...
Conclusion
Threads are a very powerful tool in developing real time applications.
However without proper planning they can result in endless grief when
synchronisation and data shearing is involved. Be sure you really need a thread
and then concentrate on avoiding spin locks and deadlocks so you threads play
well with others.
|