Multithreading in C#

In C# we can starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. First have a look at a simple example:


The main thread creates a new thread t on which it runs a method that repeatedly prints the character “HI”. Simultaneously, the main thread repeatedly prints the character “hi”. Output is:

hihihihiHIHIhiHIHIHIHIhihi….

Local variables are kept separate as the CLR assigns each thread its own memory stack. For e.g.:

Output is:

HIHIHIHIHIHIHIHIHIHI

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s