- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我最近尝试在 Visual Studio 中尝试使用 OpenMP 来学习如何对我的程序进行多线程处理。
如果我尝试串行执行此代码:
int totalSum = 0;
for(int x=0; x < 100; x++)
{
for(int y=0; y < 100; y++)
{
totalSum = totalSum + x + y;
}
}
我最终得到的是 totalSum = 990000
当我尝试通过以下方式仅添加 OpenMP 功能时:
#pragma omp parallel for
for(int x=0; x < 100; x++)
{
for(int y=0; y < 100; y++)
{
totalSum = totalSum + x + y;
}
}
我最终得到 totalSum = 491293 或 596865 或 638260 等...
显然发生的情况是竞争条件似乎正在发生,并且取决于哪个线程首先访问 totalSum,最终答案会有所不同。
我做错了什么? x 和 y 被正确定义为私有(private)变量(因为它们是在并行区域内创建的)。
与串行执行程序相比,我如何才能确保在多线程程序时得到相同的答案?
最佳答案
解决方法是使用reduction
子句:
int totalSum = 0;
#pragma omp parallel for reduction(+:totalSum) // reduction
for(int x=0; x < 100; x++)
{
for(int y=0; y < 100; y++)
{
totalSum = totalSum + x + y;
}
}
阅读 OpenMP 缩减条款,然后您就会了解它是如何工作的。
关于c - 带有 Visual Studio 的 OpenMP : race condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13301897/
我经常发现这些术语被用在并发编程的上下文中。它们是相同的还是不同的? 最佳答案 不,它们不是同一件事。它们不是彼此的子集。它们也不是彼此的必要条件,也不是充分条件。 数据竞争的定义非常明确,因此,它的
我在测试我的项目时遇到了 DATA RACE 警告,想知道是否有人愿意帮助我破译这个问题。我过去从未尝试过测试 go 例程,我发现很难全神贯注于数据竞赛。 我在描述中提供了指向未解决问题的链接,并在问
起初,我知道代码有一些竞争条件,所以我使用“go build -race”命令来检查它,我想看看结果如何显示,当我第一次运行时,它显示了第一个结果如下,然后再次运行显示第二个,它有两个不同的结果,我不
我在考虑我的代码中的极端情况,我不知道如何在检查文件是否存在时避免问题,如果不存在,则创建一个具有该文件名的文件。代码大致如下所示: // 1 status = stat(filename); if
我在考虑我的代码中的极端情况,我不知道如何在检查文件是否存在时避免问题,如果不存在,则创建一个具有该文件名的文件。代码大致如下所示: // 1 status = stat(filename); if
我想知道是否存在插入查询上实际发生竞争条件问题的真实案例。所以我有一个包含以下字段的“用户”表: User: iduser | idcompany | name | email 我为此表使用复合主键(
我有一段代码(一个测试运行器)应该运行代码并返回结果,我想为每个测试用例设置一个时间限制所以我使用 Promise.race 但不幸的是它不起作用 const createTestCafe = req
我遇到了 an implementation JavaScript 中的 Promise.race() 方法,它按预期工作,但对我来说意义不大。 const race = (...promises)
我的测试代码如下,使用threading,count不是5,000,000,所以出现data race,但是使用gevent,count是5,000,000,没有data race。 难道gevent
据我所知,关于promise有两个选项: promise.all() promise.race() 好的,我知道 promise.all() 是做什么的。它并行运行 Promise,如果两者都成功解析
又是我和我的BlockingQueue...我根据this article重写了它和 this question .它发送了一些项目,然后因访问冲突而崩溃。这是代码: template bool D
所以我有两个Python3.2进程需要相互通信。大多数需要交流的信息都是标准词典。命名管道似乎是可行的方法,所以我制作了一个可以在两个进程中实例化的管道类。这个类实现了一个非常基本的协议(protoc
我有一个注册页面,它接收 token 并解析它们并在参数适用时登录用户。 在我检查 token 的时间到我从数据库中删除 token 的时间之间,另一个用户可以使用相同的 token 登录。有没有办法
我在 redis 中有一个散列,其中一个字段的值为字符串化数组,每当用户注册一个事件时, 从redis中获取这个字符串化数组 后台解析,将用户的用户名添加到数组中 将数组字符串化并存储回哈希 如果两个
我知道之前有人问过这个问题,但我仍然很困惑,如果可能的话,我想在开始编程之前避免任何问题。 我计划拥有一个在任何给定时间至少有 100 名活跃用户的内部网站。用户将发布一个项目(以 0 作为其值插入到
我在面试中被问到以下问题。给定以下代码,如果方法 add 和 doAction 被多个线程调用,我们如何在打印 toString 时得到 NullPointerException ?** public
为什么标志“-race”的结果与预期的不一样?它期望相同的结果:1000000 - 带有标志“-race”但没有这个 https://gist.github.com/romanitalian/f403
我正在尝试了解如何为以下代码修复此竞争条件。 sayHello := func() { fmt.Println("Hello from goroutine") } go sayHello()
我有一堆 goroutines 在循环中做一些事情。我希望能够暂停所有这些,运行一些任意代码,然后恢复它们。我尝试这样做的方式可能不是惯用的(我希望有更好的解决方案),但我不明白为什么它不起作用。 精
考虑一个实现 open()、read()、write()、close()、unlocked_ioctl() 和 mmap() 的 linux 设备驱动程序。 现在,假设多个(或相同的)进程同时打开同一
我是一名优秀的程序员,十分优秀!