访问一个while循环变量,从线程中运行并在C#中使用的其他函数中使用的函数
我正在开发一个与线程相关的复杂项目。这是我的问题的最简单的解释。下面是除了主函数之外的 3 个函数的代码。所有函数都在多线程中运行。所有函数中都有一个 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 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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要提起
i
和k
从本地变量到program
类的私有字段。由于这两个字段将通过不同步的多个线程访问,因此您还应将它们声明为volatile
:访问模式的使用使使用
volatile
有些浪费。func1
是更改i
的唯一方法,因此在此方法中使用挥发性语义读取它是纯开销。您可以改用 /code> 和 。You probably need to hoist the
i
andk
from local variables to private fields of theProgram
class. Since these two fields will be accessed by more than one threads without synchronization, you should also declare them asvolatile
:The pattern of access makes the use of
volatile
a bit wasteful though. Thefunc1
is the only method that changes thei
, so reading it with volatile semantics inside this method is pure overhead. You could use instead theVolatile.Read
andVolatile.Write
methods for finer control: