小编use*_*666的帖子

OpenMP vs C++ 11线程

在以下示例中,C++ 11线程执行大约需要50秒,但OMP线程仅需5秒.有什么想法吗?(我可以向你保证,如果你正在做真正的工作而不是doNothing,或者如果你以不同的顺序进行,那么它仍然适用.)我也在16核机器上.

#include <iostream>
#include <omp.h>
#include <chrono>
#include <vector>
#include <thread>

using namespace std;

void doNothing() {}

int run(int algorithmToRun)
{
    auto startTime = std::chrono::system_clock::now();

    for(int j=1; j<100000; ++j)
    {
        if(algorithmToRun == 1)
        {
            vector<thread> threads;
            for(int i=0; i<16; i++)
            {
                threads.push_back(thread(doNothing));
            }
            for(auto& thread : threads) thread.join();
        }
        else if(algorithmToRun == 2)
        {
            #pragma omp parallel for num_threads(16)
            for(unsigned i=0; i<16; i++)
            {
                doNothing();
            }
        }
    }

    auto endTime = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = endTime - …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11

18
推荐指数
1
解决办法
1万
查看次数

为什么 OpenMP 的性能优于线程?

我一直在 OpenMP 中调用它

#pragma omp parallel for num_threads(totalThreads)
for(unsigned i=0; i<totalThreads; i++)
{
workOnTheseEdges(startIndex[i], endIndex[i]);
}
Run Code Online (Sandbox Code Playgroud)

这在 C++11 std::threads 中(我相信这些只是 pthreads)

vector<thread> threads;
for(unsigned i=0; i<totalThreads; i++)
{
threads.push_back(thread(workOnTheseEdges,startIndex[i], endIndex[i])); 
}
for (auto& thread : threads)
{
 thread.join();
}
Run Code Online (Sandbox Code Playgroud)

但是,OpenMP 实现的速度是原来的 2 倍——更快!我本来期望 C++11 线程更快,因为它们更底层。注意:上面的代码不仅被调用一次,而且可能在循环中被调用 10,000 次,所以也许这与它有关?

编辑:为了澄清,在实践中,我要么使用 OpenMP 要么使用 C++11 版本——而不是同时使用两者。当我使用 OpenMP 代码时,需要 45 秒,当我使用 C++11 时,需要 100 秒。

c++ multithreading c++11

6
推荐指数
2
解决办法
3381
查看次数

为什么在C#中进行多线程处理会使bool变得易变?

我非常了解volatileC++中的关键字.但在C#中,它似乎采用了不同的含义,与多线程更相关.我认为bool操作是原子的,我认为如果操作是原子的,你就不会有线程问题.我错过了什么?

https://msdn.microsoft.com/en-us/library/x13ttww7.aspx

c# multithreading

6
推荐指数
2
解决办法
434
查看次数

为什么将不可变列表实现为链表?

根据F#的list 文档:

  • " F#中的列表是一个有序的,不可变的同类型元素系列 "

  • " F#中的列表被实现为单链表 "

为什么不在内存中连续实现它,因为它是不可变的,因此具有固定的大小?为什么要使用F#list而不是F#array

f#

5
推荐指数
1
解决办法
418
查看次数

标签 统计

multithreading ×3

c++ ×2

c++11 ×2

c# ×1

f# ×1