Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading concept in C#

I am new to C# and still learning the threading concept. I wrote a program which is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApplication17
{
  class Program
  {
    static void Main(string[] args)
    {
        System.Threading.ThreadStart th1 = new System.Threading.ThreadStart(prnt);
        Thread th = new Thread(th1);
        th.Start();
        bool t = th.IsAlive;
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(i + "A");
        }
    }

    private static void prnt()
    {
        for (int i = 0; i < 10; i ++)
        {
            Console.WriteLine(i + "B");
        }
    }
  }
}

I am expecting an output like :-

1A
2A
1B
3A
2B...

as I believe that both main thread and newly created thread should be executing simultaneously.

But the output is ordered , first the prnt function is called and then the Main method for loop is executed.

like image 770
user2235487 Avatar asked Nov 07 '15 13:11

user2235487


People also ask

What is threading in C?

Thread-based multitasking deals with the concurrent execution of pieces of the same program. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.

Is threading possible in C?

Can we write multithreading programs in C? Unlike Java, multithreading is not supported by the language standard. POSIX Threads (or Pthreads) is a POSIX standard for threads. Implementation of pthread is available with gcc compiler.

What is threading explain with example?

Thread is often referred to as a lightweight process. The process can be split down into so many threads. For example, in a browser, many tabs can be viewed as threads. MS Word uses many threads - formatting text from one thread, processing input from another thread, etc.


2 Answers

maybe a cicle of 10 is not enough to see the two thread running at same time. use a longer loop or put a Thread.Sleep (100) within cycle iteration. Starting a thread is quite expensive. What it happens, probably, is that you Main loop get executed before thread loop start, due to the time required for a thread to start

like image 84
Gian Paolo Avatar answered Nov 04 '22 14:11

Gian Paolo


I agree with previous answers you need to add Thread.Sleep(some seconds * 1000)

I recommend you read this article about thread https://msdn.microsoft.com/ru-ru/library/system.threading.thread(v=vs.110).aspx

    static void Main(string[] args)
    {
        Thread th = new Thread(prnt);
        th.Start();
        for (int i = 0; i < 10; i++)
        {
            //sleep one second
            Thread.Sleep(1000);
            Console.WriteLine(i + "A");
        }
        //join the basic thread and 'th' thread 
        th.Join();
    }
    private static void prnt()
    {
        for (int i = 0; i < 10; i++)
        {
            //sleep one second
            Thread.Sleep(1000);
            Console.WriteLine(i + "B");
        }
    }
like image 40
Bushuev Avatar answered Nov 04 '22 14:11

Bushuev