访问一个while循环变量,从线程中运行并在C#中使用的其他函数中使用的函数

发布于 2025-01-17 16:32:18 字数 1527 浏览 0 评论 0原文

我正在开发一个与线程相关的复杂项目。这是我的问题的最简单的解释。下面是除了主函数之外的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带刺的爱情 2025-01-24 16:32:18

您可能需要提起ik从本地变量到program类的私有字段。由于这两个字段将通过不同步的多个线程访问,因此您还应将它们声明为 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的唯一方法,因此在此方法中使用挥发性语义读取它是纯开销。您可以改用 /code>

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文