C# 中最好的多线程技术是什么

发布于 2024-10-27 04:50:44 字数 1941 浏览 0 评论 0原文

我正在开发一个运行大量 TcpListener 任务的应用程序 在 Windows Server 2008 上使用 C# 和 .net 3.5

TcpListener 任务是:

  1. 监听 获取 xml 消息
  2. 读取 ID 从它
  3. 发送一个命令到 根据ID的特定相机 拍摄快照并将其存储在 文件夹

这意味着在 1 秒内执行,这样我就可以用相机拍摄快照。我使用以下代码执行此任务时出现延迟:

private Thread listen_thread;

    public void start_listen()
    {
            this.listen_thread = new Thread(new ThreadStart(save_data));
            this.listen_thread.Priority = ThreadPriority.Normal;
            this.listen_thread.Start();

    }
    private void save_data()
    {
        //work to be done
    }

这是最好的多线程技术吗?此应用程序在 Dell poweredge 2900 上运行 带有 2 个四核处理器,我认为它可以运行得更快。我怎样才能改善这段代码的延迟?

这就是 tcplistener 的代码

Int32 port = controller_port;
        try
        {
            //this server ip
            IPAddress localAddr = IPAddress.Parse(this_ip);
            server = new TcpListener(localAddr, port);
            server.Start();
            Byte[] bytes = new Byte[256];
            String data = null;
            while (true)
            {
                Console.Write("Waiting for a connection... ");
                TcpClient client = server.AcceptTcpClient();
                Console.Write("Connected!");
                data = null;
                NetworkStream stream = client.GetStream();
                int i;
                string add_data = "";
                Console.Write("Waiting...!");
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    try
                    {
                        string full_row = "";
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        Console.Write("Received from controller: " + data);
                        add_data += data;
                    }
                    catch{}
                }
            }
         }

,然后我将其除以获取 ID。

I'm developing an application that is running a lot of TcpListener tasks
using c# with .net 3.5 on windows server 2008

the TcpListener task is:

  1. listening
    to get an xml message
  2. read an ID
    from it
  3. send a command to a
    specific camera based on the ID to
    take a snapshot and store it in a
    folder

This is meant to execute within 1 second so I can take a snapshot with the camera. I've got a delay in executing this task, using the following code:

private Thread listen_thread;

    public void start_listen()
    {
            this.listen_thread = new Thread(new ThreadStart(save_data));
            this.listen_thread.Priority = ThreadPriority.Normal;
            this.listen_thread.Start();

    }
    private void save_data()
    {
        //work to be done
    }

Is this the best multi-threading technique to use? This application is running on a on dell poweredge 2900
with 2 quad core prosessor, and I think it could go faster. How might I be able to improve the latency of this code?

thats the code for the tcplistener

Int32 port = controller_port;
        try
        {
            //this server ip
            IPAddress localAddr = IPAddress.Parse(this_ip);
            server = new TcpListener(localAddr, port);
            server.Start();
            Byte[] bytes = new Byte[256];
            String data = null;
            while (true)
            {
                Console.Write("Waiting for a connection... ");
                TcpClient client = server.AcceptTcpClient();
                Console.Write("Connected!");
                data = null;
                NetworkStream stream = client.GetStream();
                int i;
                string add_data = "";
                Console.Write("Waiting...!");
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    try
                    {
                        string full_row = "";
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        Console.Write("Received from controller: " + data);
                        add_data += data;
                    }
                    catch{}
                }
            }
         }

then i divide it to get the ID .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

嘦怹 2024-11-03 04:50:44

根本不清楚

  1. 您如何接收
  2. 并行化应用程序的

请求似乎您在不同的套接字上侦听并在每个套接字上分配一个线程。

我认为这是一个糟糕的选择,因为你无法控制并行度。

您可以只使用一个线程,非确定性地侦听所有套接字,并将连接分配给线程池,配置为同时运行最多 n 个线程,其中 n 是机器的并行度(如果您有双四核,则 n 是等于8)。

It's not clear at all

  1. how you receive requests
  2. upon what you parallelize the application

It seems that you listen on different sockets and assign a thread on each of them.

I think this is a poor choice, because you can't control the parallelism degree.

You could instead have just one thread that nondeterministically listens on all sockets and assigns the connection to a thread pool, configured to simultaneously run at most n threads, where n is the parallelism degree of your machine (if you have a dual quadcore, n is equal to 8).

忆依然 2024-11-03 04:50:44

这些方法的名称明显错误。 start_listen() 不开始监听。在至少建立连接之前,save_data() 不可能保存任何数据。这需要客户端代码首先启动并建立连接。当然,一秒钟很快就过去了。当有可用核心时,启动线程不会超过几分之一毫秒。

重点关注代码片段中缺少的代码。

These methods have strikingly wrong names. start_listen() doesn't start listening. save_data() cannot possibly save any data until at least a connection is established. Which requires the client code to start up first and make the connection. Sure, one second is quickly gone with that. Starting a thread doesn't take more than a fraction of a millisecond when there's a core available.

Focus on the code that's missing in your snippet.

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