-1

I have two threads. How to get data from thread1 to thread2. It means, whe thread1 has done its work, it has some data, and this data must be used in the second "thread2". How to realize it ?

Here is code, but what to do..now ?

static void Main(string[] args)
        {
            Thread t1 = new Thread(thread1);
            t1.Start();
            Thread t2 = new Thread(thread2);
            t2.Start();
        }
        static void thread1()
        {
                string newstring="123";

        }
        static void thread2()
        {
            //what to do here...what code will be here?
            Console.WriteLine(newstring);
        }

In thread1 can be whatever, but i need to get this "whatever", than i can use it in thread2

P. Dock
  • 9
  • 1
  • Any suggestions? – P. Dock May 21 '18 at 02:07
  • Your question really has nothing to do with threads. That code wouldn't work even if there was only one thread because of scope, i.e. a variable declared within a method is only accessible in that method. If you declare that variable at the class level then it will work, whether there are multiple threads or just one. – jmcilhinney May 21 '18 at 02:11
  • @jmcilhinney i really dont know, how to do this! Therefore i have asked question like this, hope that someone helps me. – P. Dock May 21 '18 at 02:12
  • The difference when dealing with multiple threads is order of execution. If you have one thread then you can ensure that one method is executed before the other by calling them in that order. If methods are executed on different threads then there's no guarantee that they will be executed in the order you expect or even the same order every time. Thread synchronisation is a big topic in itself, so we're not going to cover that here. – jmcilhinney May 21 '18 at 02:14
  • 2
    "i really dont know, how to do this". Then learn. We're not here to teach you the basics of programming or of VB. Any beginner tutorial will teach you about variables so you should have already covered that well before trying to use mutli-threading. The thing is, you DO know how to do it. It's not rocket science. I said "a variable declared WITHIN a method is only accessible in that method". You know what "within" means so you know what it would mean to declare a variable NOT within a method. These words mean the same here as elsewhere. – jmcilhinney May 21 '18 at 02:19
  • When you have a problem that you think will be solved by using threads you then now have two problems. – Enigmativity May 21 '18 at 03:12
  • What is it that you're actually trying to do? – Enigmativity May 21 '18 at 03:12

1 Answers1

2

Data, which is used by both Thread must be commonly shared between both thread.

usually it is called common resource.

One this you must note that you have to achieve synchronization here.

As both threads are running independently and also reading/writing common data, chances of Race Condition is pretty high. To prevent such cases, you must implement synchronization on reading/writing data (on common object).

refere below code, where CommonResource is common between both threads and synchronization has been achieved by locking

In your example, one thread is writing data and other thread is reading data. If we don't implement Synchronization, there are chances that while thread 1 is writing new data, but thread 2 (because it is not waiting for thread 1 to complete it's task first) will bring old data (or invalid data).

Situation goes worst when there are multiple threads which are writing data, without waiting for other threads to complete their writing.

public class CommonResourceClass
{
    object lockObj;
    //Note: here main resource is private 
    //(thus not in scope of any thread)
    string commonString;

    //while prop is public where we have lock
    public string CommonResource
    {
        get
        {
            lock (lockObj)
            {
                Console.WriteLine(DateTime.Now.ToString() + " $$$$$$$$$$$$$$$ Reading");
                Thread.Sleep(1000 * 2); 
                return commonString;
            }
        }
        set
        {
            lock (lockObj)
            {
                Console.WriteLine(DateTime.Now.ToString() + " ************* Writing");
                Thread.Sleep(1000 * 5); 
                commonString = value;
            }
        }
    }

    public CommonResourceClass()
    {
        lockObj = new object();
    }
}

and Thread calling be like

    static CommonResourceClass commonResourceClass;
    static void Main(string[] args)
    {
        commonResourceClass = new CommonResourceClass();
        Thread t1 = new Thread(ThreadOneRunner);
        Thread t2 = new Thread(ThreadTwoRunner);
        t1.Start();
        t2.Start();
    }

    static void ThreadOneRunner()
    {
        while(true)
        {
            Console.WriteLine(DateTime.Now.ToString() + " *******Trying To Write");
            commonResourceClass.CommonResource = "Written";
            Console.WriteLine(DateTime.Now.ToString() + " *******Writing Done");
        }
    }

    static void ThreadTwoRunner()
    {
        while(true)
        {
            Console.WriteLine(DateTime.Now.ToString() + " $$$$$$$Trying To Read");
            string Data = commonResourceClass.CommonResource;
            Console.WriteLine(DateTime.Now.ToString() + " $$$$$$$Reading Done");
        }
    }

Output of it:

enter image description here

Note That, reading is taking 2 seconds and writing is taking 5 seconds, so reading is supposed to be faster. But if writing is going on, reading must wait till writing done.

you can clearly see in output, as one thread is trying to read or write, it cannot do it while other thread is performing it's task.

Amit
  • 1,821
  • 1
  • 17
  • 30