如何更改多线程的单线程函数/方法 [英] How to change a Single Thread Function/ Method For Multi Thread

查看:152
本文介绍了如何更改多线程的单线程函数/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我目前有一个面向Tsp求解的遗传算法,我想改变遗传算法的某些部分来运行Multi-线程。



我想改为多线程的第一个功能是健身功能。

有人可以给我一些提示吗在多线程中运行适应度函数所需的更改?

Hello,

I have at the moment a Genetic Algorithm oriented for Tsp Solving, and I whant to change some part's of the Genetic Algorithm to run in Multi-Thread.

The first function that I whant to change to Multi-Threading is the fitness function.
Can someone give me some tips about the changes needed for running the fitness function in multi thread?

public void CalculateCityDistances( int numberOfCloseCities )
        {
            foreach (City city in this)
            {
                city.Distances.Clear();

                for (int i = 0; i < Count; i++)
                {
                    city.Distances.Add(Math.Sqrt(Math.Pow((double)(city.Location.X - this[i].Location.X), 2D) +
                                       Math.Pow((double)(city.Location.Y - this[i].Location.Y), 2D)));
                }
            }


 public void DetermineFitness(Cities cities)
        {
            Fitness = 0;

            int lastCity = 0;
            int nextCity = this[0].Connection1;

            foreach (Link link in this)
            {
                Fitness += cities[lastCity].Distances[nextCity];

                // figure out if the next city in the list is [0] or [1]
                if (lastCity != this[nextCity].Connection1)
                {
                    lastCity = nextCity;
                    nextCity = this[nextCity].Connection1;
                }
                else
                {
                    lastCity = nextCity;
                    nextCity = this[nextCity].Connection2;
                }
            }
        }



感谢您阅读我的帖子。


Thanks for reading my post.

推荐答案

这不是线程的工作原理。这不是改变方法。您需要创建或获取一个线程并从线程调用您的方法。启动它时,该线程将起作用。但主要关注的是线程同步,特别是在线程之间共享对象。这应该减少到必要的最低限度,但在大多数情况下是不可避免的。它包括 lock 语句,线程同步对象(primitives),调用委托给UI线程( Invoke BegingInvoke 的方法System.Windows.Threading.Dispatcher System.Windows.Forms.Control 等等。我认为你需要自己学习它,因为单个快速回答会有点太多了。 br $>


请参阅 System.Threading.Thread

http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx [ ^ ]。



要创建或获取其他线程,您可以
This is not how threading works. It's not "changing method". You need to create or obtain a thread and call your method from the thread. The thread will work when you start it. But the major concern is the thread synchronization, in particular, sharing objects between threads. This should be reduced to necessary minimum, but in most cases unavoidable. It includes the lock statement, thread synchronization objects ("primitives"), invocation delegates to the UI thread (Invoke and BegingInvoke methods of System.Windows.Threading.Dispatcher or System.Windows.Forms.Control and more. I think you need to learn it all by yourself, as it would be a bit too much for a single Quick Answer.

Please see System.Threading.Thread:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[^].

To create or obtain an additional thread, you can
  1. 使用其构造函数创建线程实例:

    http://msdn.microsoft.com/en -us / library / xx3ezzs2.aspx [ ^ ]。



    我想阻止使用 ParametrizedThreadStart ,因为它涉及类型转换。更好的方法是创建一个线程包装器,其中this参数隐式用于访问整个包装器实例。它还允许透明地封装线程同步等。请看我过去的答案:

    如何将ref参数传递给线程 [ ^ ],

    更改线程(生产者)启动后的参数 [ ^ ],

    C#中的多线程 [ ^ ]。
  2. 您可以从线程池获取线程。请参阅:

    http://en.wikipedia.org/wiki/Thread_pool_pattern [< a href =http://en.wikipedia.org/wiki/Thread_pool_patterntarget =_ blanktitle =New Window> ^ ],

    http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx [ ^ ] 。
  3. 此外,您可以使用类 System.ComponentModel.BackgroundWorker

    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx [ ^ ]
  1. Create a thread instance with its constructor:
    http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx[^].

    I want to discourage using ParametrizedThreadStart as it involves type cast. Much better approach involves creating a thread wrapper, where "this" parameter is implicitly used to access the whole wrapper instance. It also allows to transparently encapsulate thread synchronization and more. Please see my past answers:
    How to pass ref parameter to the thread[^],
    Change parameters of thread (producer) after it is started[^],
    MultiThreading in C#[^].
  2. You can obtain a thread from the thread pool. Please see:
    http://en.wikipedia.org/wiki/Thread_pool_pattern[^],
    http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[^].
  3. Also, you can use the class System.ComponentModel.BackgroundWorker:
    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]





作为替代品如果要进行线程化,您可以考虑使用基于线程的.NET Parallel Extensions,但不会明确使用线程,并且解决方案可能显示为单线程,透明。



请参阅:

http://en.wikipedia.org/wiki/Parallel_Extensions [ ^ ],

http://msdn.microsoft.com/en-us/library/system.threading.tasks.aspx [ ^ ],

http://msdn.microsoft .com / zh-cn / library / system.threading.tasks.parallel.aspx [ ^ ]。



另请参阅我过去的线程答案:

如何获得keydown事件在vb.net中的另一个线程上运行 [ ^ ],

启用禁用+多线程后控制事件未触发 [ ^ ]。







-SA



As the alternative to threading, you can consider using .NET Parallel Extensions, which is based on threading, but threads are not used explicitly and the solutions may appear single-threaded, transparently.

Please see:
http://en.wikipedia.org/wiki/Parallel_Extensions[^],
http://msdn.microsoft.com/en-us/library/system.threading.tasks.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.aspx[^].

See also my past answers on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].



—SA


问题解决了。



问题是从一个版本迁移到另一个版本。
Problem solved .

The problem was the migration from one version to another.


这篇关于如何更改多线程的单线程函数/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆