0

目前我有一个正在运行的骑士之旅算法。

我使用以下组合:

  • 回溯
  • 华恩斯多夫规则

该算法执行以下操作:

Checks to see if the board is solved (all squares visited) 
    If true: return true
else proceed:

Make a set of possible moves from current positions
Sort the set based on the number of moves available from those positions.

Go through the set. (Recursive call made here)
    If returned True:
       Set Solution board to appropriate number
       Return true
    else
      go back to last position
      return false.

它工作正常。这不是最好的解决方案。

我正在尝试使用并行化来提高速度,特别是使用 C++ 线程 ( #include<thread>)。

这是什么算法?到目前为止,我尝试过的唯一方法有错误共享问题、共享内存问题或根本无法运行。

4

1 回答 1

1

当然这是针对 C++ 的,在调用#include<thread>标头之后,创建线程的简单方法是:

#include <iostream>
#include <thread>

void testThread()
{
    std::cout<<"Thread\n";
}

int main(int argc, char * argv[])
{
    std::testThread t(testThread);
    t.join();

    return 0;
}

完成后在加入std::testThread t(testThread);线程时调用线程创建t.join();。但这一切都没有使用锁。如果我是你,我会在网上查看相同的示例 - 有大量资源 - 展示如何在安全庄园中实施锁。

需要注意的是,您必须确保代码的顺序版本实际上可以从并行运行中受益,因为创建线程的成本可能很高。

于 2015-12-06T04:35:45.660 回答