top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C : Write a multi threaded program program in C language ?

+2 votes
265 views
C : Write a multi threaded program program in C language ?
posted Nov 14, 2015 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Program 1

using System;
using System.Threading;

public class MyThread {

        public static void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread1 {0}", i);
                }
        }

        public static void Thread2() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread2 {0}", i);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1 ) );
                Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2 ) );

                tid1.Start();
                tid2.Start();
        }
}

Let's explore the the whole program.

This program has a class MyThread which has two static functions Thread1 and Thread2. To make a thread you have to make an object of class Thread. The constructor of this class takes a reference of a ThreadStart class. This constructor can send two types of exceptions; ArgumentNullException when the parameter is a null reference or a Security Exception when program does not have permission to create thread.

The parameter of the Thread class is reference to a ThreadStart class. ThreadStart class points to the method that should be executed first when a thread is started. The parameter is the name of the function, which is considered as a thread function. Thread1 is a static function so we give it with the name of class name without making an object of the class. The thread starts execution with Start() method of the Thread class. The output of this program is

Before start thread
Thread1 0
Thread1 1
Thread1 2
Thread1 3
Thread1 4
Thread1 5
Thread1 6
Thread1 7
Thread1 8
Thread1 9
Thread2 0
Thread2 1
Thread2 2
Thread2 3
Thread2 4
Thread2 5
Thread2 6
Thread2 7
Thread2 8
Thread2 9

It is not a requirement that thread function must be static. We can make it a non-static function but in this case we have to create object of that class.

Program 2

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread1 {0}", i);
                }
        }

        public void Thread2() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread2 {0}", i);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

        MyThread thr = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr.Thread2) );

                tid1.Start();
                tid2.Start();
        }
}

The output of this program is same as previous one.

It is not necessary to make two functions for making two threads. You may have only one thread function and create two threads by creating two objects of the thread class. Take a look at this program.

Program 3

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world {0}", i);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}

Here we create two objects of class MyThread to execute two threads.

It is also not necessary to create the object of ThreadStart at the time of passing paramaters in the constructor. You can create the object of ThreadStart and pass it as a parameter of Thread. Let's see this program.

Program 4

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world {0}", i);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

        MyThread thr1 = new MyThread();
        MyThread thr2 = new MyThread();

        ThreadStart tStart1 = new ThreadStart(thr1.Thread1);
        ThreadStart tStart2 = new ThreadStart(thr2.Thread1);

                Thread tid1 = new Thread(tStart1);
                Thread tid2 = new Thread(tStart2);

                tid1.Start();
                tid2.Start();
        }
}

This program also gives the same output.

The Sleep method suspends the execution of the thread for a specified time. Sleep is a static method so it can be used without making an object of Thread. There are two overloaded versions of Sleep(), one function takes time in milliseconds and second method take a reference of an instance of the TimeSpan structure.

Program 5

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world " + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}

Now the thread sleeps for one millisecond and gives the second thread an opportunity to execute. The output of this program is

Before start thread
Hello world 0
Hello world 0
Hello world 1
Hello world 1
Hello world 2
Hello world 2
Hello world 3
Hello world 3
Hello world 4
Hello world 4
Hello world 5
Hello world 5
Hello world 6
Hello world 6
Hello world 7
Hello world 7
Hello world 8
Hello world 8
Hello world 9
Hello world 9

Now both threads seem to execute in parallel. Here is a program which shows the usage of the other overloaded method of sleep.

Program 6

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        int iHour = 0;
                        int iMin = 0;
                        int iSec = 1;

                        Console.WriteLine("Hello world " + i);
                        Thread.Sleep(new TimeSpan(iHour, iMin, iSec) );
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}

The TimeSpan structure has four oveloaded constructors. The first take only one long paramater to a specified number of ticks, the second takes three int paramters for hours, mins and seconds respectively, the third take three int parameters for days, hours, mins and seconds and fourth overloaded constructor takes five parameter for days, hours, mins, seconds and milliseconds. The output of this program is the same except this program prints a new value after one second.

The Sleep method throws three type of exceptions; ArgumentException, when the time-out value is less than zero, ThreadInterruptedException when the thread is interrupted while sleeping and SecurityException when caller don’t have the appropriate permissions.

Let's see the program showing to handle the situation when the argument of Sleep() is negative.

Program 7

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        int iHour = 0;
                        int iMin = 0;
                        int iSec = -1;

                        try {
                                Console.WriteLine("Hello world " + i);
                                Thread.Sleep(new TimeSpan(iHour, iMin, iSec) );
                        }
                        catch (ArgumentException ae) {
                                Console.WriteLine(ae.Message );
                        }

                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}

Message is a property of the ArgumentException class giving the description of the exception. In this program it gives the error message

Parameter Name: Argument must be greater than 0 and less than 2^31 - 1milliseconds.

We can also assign a name to thread by using Name property of Thread.

answer Nov 17, 2015 by Shivaranjini
...