代码之家  ›  专栏  ›  技术社区  ›  huseyin tugrul buyukisik

C++:如何在同一范围内多次对宏使用并行?

  •  0
  • huseyin tugrul buyukisik  · 技术社区  · 9 年前

    我使用局部函数定义运行代码块N次,就像C#版本的并行程序一样,但它为 f 当我在同一范围内使用两个或多个函数时。在同一范围内每次使用后,如何使函数名增量更改,如f1、f2、f3等?


    定义:

    #ifndef PARALLEL_FOR
    #define PARALLEL_FOR(N,O) \
                            struct LocalClass                                                           \
                            {                                                                           \
                                void operator()(int i) const O                                      \
                            } f;                                                                        \
                            std::thread threads[N];                                                     \
                            for(int loopCounterI=0; loopCounterI<N; loopCounterI++)                     \
                            {                                                                           \
                                threads[loopCounterI]=std::thread(f,loopCounterI);                      \
                            }                                                                           \
                            for(int loopCounterI=0; loopCounterI<N; loopCounterI++)                     \
                            {                                                                           \
                                threads[loopCounterI].join();                                           \
                            }                                                                           \
    
    #endif
    

    用法:

    PARALLEL_FOR(
            5,
            {std::cout<<100*i<<std::endl;}
    );
    

    0
    300
    200
    100
    400
    

    正常添加 {\ 和 }\

    2 回复  |  直到 9 年前
        1
  •  5
  •   O'Neil    9 年前

    您可以使用 __LINE__ .

    template <int N, class F>
    void parallel_for(F f) {
        std::thread threads[N]; // or std::array<std::thread, N> threads;
    
        for(int loopCounterI=0; loopCounterI<N; loopCounterI++) {
            threads[loopCounterI]=std::thread(f,loopCounterI);
        }
        for(int loopCounterI=0; loopCounterI<N; loopCounterI++) {
            threads[loopCounterI].join();
        }
    }
    

    使用方式如下:

    parallel_for<5>([] (int i) {
        std::cout<<100*i<<std::endl;
    });
    

    lambda可以捕获您需要的变量:

    int j = ...;
    parallel_for<5>([j] (int i) {
        std::cout<<100*(i+j)<<std::endl;
    });
    
        2
  •  2
  •   Daniel    9 年前

    虽然这似乎不是理想的解决方案,但可以使用 ## .在代码中执行以下操作

    #ifndef PARALLEL_FOR
    #define PARALLEL_FOR(N,O,usageInd) \
                        struct LocalClass##usageInd                                                           \
                        {                                                                           \
                            void operator()(int i) const O                                      \
                        } f##usageInd;                                                                        \
                        std::thread threads[N];                                                     \
                        for(int loopCounterI=0; loopCounterI<N; loopCounterI++)                     \
                        {                                                                           \
                            threads[loopCounterI]=std::thread(f##usageInd,loopCounterI);                      \
                        }                                                                           \
                        for(int loopCounterI=0; loopCounterI<N; loopCounterI++)                     \
                        {                                                                           \
                            threads[loopCounterI].join();                                           \
                        }                                                                           \
    
    #endif
    

    推荐文章