<small id='DSVG9'></small><noframes id='DSVG9'>

    • <bdo id='DSVG9'></bdo><ul id='DSVG9'></ul>

        <tfoot id='DSVG9'></tfoot>
        <legend id='DSVG9'><style id='DSVG9'><dir id='DSVG9'><q id='DSVG9'></q></dir></style></legend>
      1. <i id='DSVG9'><tr id='DSVG9'><dt id='DSVG9'><q id='DSVG9'><span id='DSVG9'><b id='DSVG9'><form id='DSVG9'><ins id='DSVG9'></ins><ul id='DSVG9'></ul><sub id='DSVG9'></sub></form><legend id='DSVG9'></legend><bdo id='DSVG9'><pre id='DSVG9'><center id='DSVG9'></center></pre></bdo></b><th id='DSVG9'></th></span></q></dt></tr></i><div id='DSVG9'><tfoot id='DSVG9'></tfoot><dl id='DSVG9'><fieldset id='DSVG9'></fieldset></dl></div>

      2. 我可以创建一个线程安全的 std::atomic<vector<

        时间:2023-10-07

              <tbody id='4N62S'></tbody>
            <tfoot id='4N62S'></tfoot>

            <i id='4N62S'><tr id='4N62S'><dt id='4N62S'><q id='4N62S'><span id='4N62S'><b id='4N62S'><form id='4N62S'><ins id='4N62S'></ins><ul id='4N62S'></ul><sub id='4N62S'></sub></form><legend id='4N62S'></legend><bdo id='4N62S'><pre id='4N62S'><center id='4N62S'></center></pre></bdo></b><th id='4N62S'></th></span></q></dt></tr></i><div id='4N62S'><tfoot id='4N62S'></tfoot><dl id='4N62S'><fieldset id='4N62S'></fieldset></dl></div>
              • <legend id='4N62S'><style id='4N62S'><dir id='4N62S'><q id='4N62S'></q></dir></style></legend>

                • <bdo id='4N62S'></bdo><ul id='4N62S'></ul>
                • <small id='4N62S'></small><noframes id='4N62S'>

                  本文介绍了我可以创建一个线程安全的 std::atomic<vector<int>> 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个函数需要执行 n=1000 次.这个函数进行蒙特卡罗风格的模拟并返回一个 int 作为结果.我想并行运行 nthreads=4.每当一个线程完成一个循环时,它应该将结果放入std::vector.因此,在 1000 个周期后,我有一个可以通过统计检查的 1000 个 int 向量.

                  I'm having a function that needs to be executed n=1000 times. This functions does a Monte Carlo style simulation and returns an int as the result. I'd like to run nthreads=4 in parallel. Whenever a thread finishes one cycle, it should put the result in a std::vector<int>. Thus, after 1000 cycles, I've a vector of 1000 ints that can be examined by statistics.

                  由于 std::vector 不是线程安全的,我想到了 std::mutex(这肯定会工作).

                  Since a std::vector is not thread-safe, I thought about std::mutex (which would surely work).

                  但是我想知道是否可以将向量声明为原子向量从而绕过互斥锁?是否可以有 std::atomic>?我可以在上面使用 push_back 等吗?

                  But I wonder if I can declare a vector to be atomic and thus get around mutexes? Is it possible to have a std::atomic<std::vector<int>>? And can I use push_back etc. on it?

                  推荐答案

                  你不需要.从多个线程访问 std::vector 是完全可以的,if

                  You don't need to. It is totally okay to access a std::vector from multiple threads, if

                  • 你读取对象
                  • 你写给不同的对象

                  因此,请确保创建一个大小为 n=1000 的向量,并根据您的线程编号(1 到 4)将元素 0-249、250-499 等分配给您的线程.

                  So just make sure, you create a vector of size n=1000 and depending on your thread number (1 to 4) you assign elements 0-249, 250-499 etc. to your threads.

                  所以你的每个线程都会计算 n/nthreads 个元素.

                  So each of your thread computes n/nthreads elements.

                  这篇关于我可以创建一个线程安全的 std::atomic<vector<int>> 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 C++ 中,按第一个元素然后按第二个元素对成对 下一篇:尝试从 Derived* 的向量中分配 Base* 的向量

                  相关文章

                  最新文章

                • <small id='Nijlp'></small><noframes id='Nijlp'>

                  <i id='Nijlp'><tr id='Nijlp'><dt id='Nijlp'><q id='Nijlp'><span id='Nijlp'><b id='Nijlp'><form id='Nijlp'><ins id='Nijlp'></ins><ul id='Nijlp'></ul><sub id='Nijlp'></sub></form><legend id='Nijlp'></legend><bdo id='Nijlp'><pre id='Nijlp'><center id='Nijlp'></center></pre></bdo></b><th id='Nijlp'></th></span></q></dt></tr></i><div id='Nijlp'><tfoot id='Nijlp'></tfoot><dl id='Nijlp'><fieldset id='Nijlp'></fieldset></dl></div>

                • <tfoot id='Nijlp'></tfoot>
                • <legend id='Nijlp'><style id='Nijlp'><dir id='Nijlp'><q id='Nijlp'></q></dir></style></legend>

                    • <bdo id='Nijlp'></bdo><ul id='Nijlp'></ul>