Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
4Activity
0 of .
Results for:
No results containing your search query
P. 1
Threads Events Mutexes

Threads Events Mutexes

Ratings: (0)|Views: 140|Likes:
Published by api-3810166

More info:

Published by: api-3810166 on Oct 17, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

07/07/2014

pdf

text

original

Threads, Events and Mutexes

Threads operate on the premise that the computer is definitely more expeditious than a human at executing tasks, resulting in the computer idling away most of its processing time, waiting for the human operator. Threads are a means of overcoming this wastage of processing time. They perform multiple tasks on a computer in rapid succession, thereby, creating the illusion of these tasks being executed simultaneously. No application can ever be comprehensive without employing threads. Before we try to infer what a thread does in the programming context, let us rummage through a few examples given below.

Without being discursive, let us venture out on our odyssey of understanding the
concept of a thread with the assistance of a very diminutive program.

a.cs
using System.Threading;
public class yyy
{public static void abc()

{System.Console.WriteLine("Hi");
}}public class zzz
{public static void Main()
{ThreadStart ts = new ThreadStart(yyy.abc);

Thread t = new Thread(ts);
System.Console.WriteLine("Before Start");
t.Start();
}}Output

Before Start
Hi

This one is bound to leave you astonished because, we had earlier talked about starting out with a 'dimunitive' program. However, by no stretch of the imagination can the above program qualify as miniscule. Besides, the only work accomplished by this function is that it calls the static function abc, which in turn displays 'Hi'.

In Main, we create an object ts, which is an instance of the class ThreadStart, which is derived from Delegate. Therefore, even though ThreadStart is a class, it also happens to be a delegate, whose constructor is given a static function called abc. Function abc is placed in the yyy class so that other classes can also use it. The program will work in a similar manner even if the static function is placed in class zzz.

Next, we create another object t, which is an instance of Thread. The constructor of this object is given a ThreadStart object ts. Indirectly, ts stands for the static function yyy.abc since it is a delegate.

So far, tranquility prevails and nothing transpires. The function yyy.abc too does not get
called. But, as soon as we call Start off the thread, the function abc gets catapulted into

action. Thus, the function abc is called only when the Start function is called. This is really no big deal. Note that classes beginning with Thread belong to the System.Threading namespace.

a.cs
using System.Threading;
public class yyy
{public void abc()

{System.Console.WriteLine("Hi");
}}public class zzz

{public static void Main()
{yyy a = new yyy();
Thread t = new Thread(new ThreadStart(a.abc));

t.Start();
}}Output
Hi

The above program is similar to the previous one and resembles the samples supplied by Microsoft. The function abc to be called, is non-static and hence, an object name is needed to reference it. The ThreadStart delegate object is directly passed as a parameter to the Thread constructor.

a.cs
using System.Threading;
public class yyy
{public void abc()

{for ( int i = 0; i<=3;i++)
{System.Console.Write(i + " ");
}}public void pqr()

{for ( int i = 0; i<=3;i++)
{System.Console.Write(i+ "...");
}}}public class zzz

{public static void Main()
{yyy a = new yyy();
Thread t = new Thread(new ThreadStart(a.abc));

Thread t1 = new Thread(new ThreadStart(a.pqr));

t.Start();
t1.Start();
}}Output

0 1 2 3 0...1...2...3...

The example embodies a very large amount of code. However, it does not create any fresh ripples in our pond of knowledge. We have merely created two Thread objects t and t1, and passed their constructors a different delegate or function name, i.e. abc and pqr, respectively. Thereafter, the Start function has been called in t and t1. Here, the function abc gets called, which displays four numbers. Thereafter, the function pqr gets called, which also displays four numbers, but with three dots.

You may wonder with trepidation as to when you will bite into the real meat. Keep your
impatience in abeyance for a little while !

a.cs
using System.Threading;
public class yyy
{public void abc()

{for ( int i = 0; i<=3;i++)
{System.Console.Write(i + " ");
Thread.Sleep(1);

}}public void pqr()

{for ( int i = 0; i<=3;i++)
{System.Console.Write(i+ "...");
Thread.Sleep(1);

}}}public class zzz
{public static void Main()
{yyy a = new yyy();

Thread t = new Thread(new ThreadStart(a.abc));
Thread t1 = new Thread(new ThreadStart(a.pqr));
t.Start();
t1.Start();
}}Output

0 0...1 1...2 2...3 3...

Activity (4)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
Lon Chu liked this
grohit1 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->