使用线程解决 N 个皇后难题
Solving N queens puzzle using pthreads
我知道如何使用回溯和递归来解决n个皇后难题。
我在想如何使用多线程来优化它。
我正在尝试使用 p - 线程。
基本上我无法理解顶部应用线程的位置以及在哪里加入线程。
由于这是递归,我也无法理解线程在这里如何工作。
--
谢谢
阿洛克·
一种方法是使用队列将每个扩展放入队列中,而不是执行递归。有一个线程池来弹出扩展并对其进行处理:
create a state with an empty board and put it into the queue
create N threads with the following function
螺纹功能:
while not done:
1) pop a state S from the queue (use locks), if queue is empty,
wait on a semaphore until there is an S
2) expand state S
2a) if S has feasible children then put them into the queue
except for one state SS, call it S and goto 2
(also signal the semaphore)
2b) if S has no feasible children goto 1
end while
您可以针对不同的算法修改此设置
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- 为什么我不能在 while 循环中创建线程?