Home > Back-end >  Access a while looped variable from a function that is running in thread and use in other function i
Access a while looped variable from a function that is running in thread and use in other function i

Time:03-30

I am working on a complex project that is related to threading. Here is the simplest interpretation of my problem. Below is the code here are 3 functions excluding the main function. All functions are running in multi-threads. There is a while loop in all functions. I just want to get variable "i" and "k" from "func1" and "func2" respectively and use it in "func3". These variables are updated in the while loop. This is the code

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


namespace Threading
{
    class Program
    {
        public static void func1() //How can I get the variable "i" from the while loop. Note: This function is running in thread.
        {
            int i = 1;
            while (true)
            {
                Console.WriteLine("Func1: "   i);
                i  ;
            }
        }

        public static void func2() //How can I get the variable "k" from the while loop. Note: This function is running in thread.
        {
            int k = 1;
            while (true)
            {
                Console.WriteLine("Func2: "   k);
                k  ;
            }
        }

        public static void func3() //After getting variables from func1 and func2 I want them to use in function 3.
        {
            while (true)
            {
                int sum = i   k;
                Console.WriteLine("the sum is"   sum);
            }
        }

        public static void Main(string[] args)
        {
            Thread t1 = new Thread(func1);
            Thread t2 = new Thread(func2);
            Thread t3 = new Thread(func3);
            t1.Start();
            t2.Start();
            t3.Start();
        }
    }
}

CodePudding user response:

You probably need to hoist the i and k from local variables to private fields of the Program class. Since these two fields will be accessed by more than one threads without synchronization, you should also declare them as volatile:

private static volatile int i = 1;
private static volatile int k = 1;

public static void func1()
{
    while (true)
    {
        Console.WriteLine("Func1: "   i);
        i  ;
    }
}

public static void func2()
{
    while (true)
    {
        Console.WriteLine("Func2: "   k);
        k  ;
    }
}

public static void func3()
{
    while (true)
    {
        int sum = i   k;
        Console.WriteLine("the sum is"   sum);
    }
}

The pattern of access makes the use of volatile a bit wasteful though. The func1 is the only method that changes the i, so reading it with volatile semantics inside this method is pure overhead. You could use instead the Volatile.Read and Volatile.Write methods for finer control:

private static int i = 1;
private static int k = 1;

public static void func1()
{
    while (true)
    {
        Console.WriteLine("Func1: "   i);
        Volatile.Write(ref i, i   1);
    }
}

public static void func2()
{
    while (true)
    {
        Console.WriteLine("Func2: "   k);
        Volatile.Write(ref k, k   1);
    }
}

public static void func3()
{
    while (true)
    {
        int sum = Volatile.Read(ref i)   Volatile.Read(ref k);
        Console.WriteLine("the sum is"   sum);
    }
}
  • Related