从线程中 运行 的函数访问 while 循环变量并在 C# 中的其他函数中使用
Access a while looped variable from a function that is running in thread and use in other function in C#
我正在从事一个与线程相关的复杂项目。这是对我的问题的最简单解释。下面是代码,这里有 3 个函数,不包括 main 函数。所有函数在多线程中都是运行。所有函数中都有一个 while 循环。我只想分别从“func1”和“func2”中获取变量“i”和“k”,并在“func3”中使用它。这些变量在 while 循环中更新。
这是代码
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();
}
}
}
您可能需要将 i
和 k
从局部变量提升到 Program
class 的私有字段。由于这两个字段会在不同步的情况下被多个线程访问,所以你也应该将它们声明为 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);
}
}
访问模式使得 volatile
的使用有点浪费。 func1
是唯一改变 i
的方法,因此在该方法中使用可变语义读取它是纯粹的开销。您可以改用 Volatile.Read
and Volatile.Write
方法进行更精细的控制:
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);
}
}
我正在从事一个与线程相关的复杂项目。这是对我的问题的最简单解释。下面是代码,这里有 3 个函数,不包括 main 函数。所有函数在多线程中都是运行。所有函数中都有一个 while 循环。我只想分别从“func1”和“func2”中获取变量“i”和“k”,并在“func3”中使用它。这些变量在 while 循环中更新。 这是代码
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();
}
}
}
您可能需要将 i
和 k
从局部变量提升到 Program
class 的私有字段。由于这两个字段会在不同步的情况下被多个线程访问,所以你也应该将它们声明为 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);
}
}
访问模式使得 volatile
的使用有点浪费。 func1
是唯一改变 i
的方法,因此在该方法中使用可变语义读取它是纯粹的开销。您可以改用 Volatile.Read
and Volatile.Write
方法进行更精细的控制:
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);
}
}