【问题标题】:What is the internal container used in tbb::concurrent_bounded_queue?tbb::concurrent_bounded_queue 中使用的内部容器是什么?
【发布时间】:2021-07-06 08:05:26
【问题描述】:

我知道 std::queue 默认使用 std::deque 作为其内部容器。我找不到 TBB 的相同信息。

我有一个遗留的多线程应用程序,该应用程序当前使用围绕 std::queue> 的线程安全包装器来存储相对较大的对象(58 字节)。我目前正在寻找更好的替代方案来提高性能。

一种选择是摆脱链表并使用默认的 std::deque 作为内部容器,并从指向对象的指针切换到按值存储对象。分块分配的 std::deque 将在内存方面更好地扩展,因为没有。的元素增加。从缓存的角度来看,还有一些连续的元素会很有帮助。

另一个选项是使用 TBB 的 concurrent_bounded_queue。但是我没有足够的信息来知道将我的对象存储为值是否是一个可行的选择。

也欢迎任何替代建议。

【问题讨论】:

  • 如果您可以按值存储对象,为什么您目前使用void*
  • @JohnZwinck 正如我所提到的,它的遗留代码是我继承来优化的。
  • tbb::concurrent_bounded_queue 不是像 std::queue 这样的适配器
  • @SuhailKhan 那么你应该做的第一件事就是摆脱void*

标签: c++ stl tbb intel-oneapi stddeque


【解决方案1】:

您可以将对象作为值存储在 tbb::concurrent_bounded_queue 中。 可以参考下面的示例代码来实现。

#include <tbb/concurrent_queue.h>
#include <tbb/concurrent_priority_queue.h>
#include <iostream>
    static int value=0;
    static int obj_count=0;       // count of objects 
class Myclass{
    public:

    int myarray[10];
    Myclass()
    {
        for(int i=0;i<10;i++){
            myarray[i]=value++;   //initializing the values of myarray for each new object
        }
        
    }
void show()
{
    std::cout<< " Values of object "<< (++obj_count ) <<" are: ";
    for(int i=0;i<10;i++){
        std::cout<<myarray[i]<<" "; // printing the data values of myarray object
    }
    std::cout<<std::endl;
}   
};

int main()
{
    Myclass m[10];
    tbb::concurrent_bounded_queue<Myclass> queue;  // creatiing a concurrent_bounded_queue of type "Myclass"
    for(int i=0;i<10;++i){
        queue.try_push(m[i]);  //pushing each Myclass object into the concurrent_bounded_queue
    }
    for(int i=0;i<10;i++){
    Myclass val;
        if(queue.try_pop(val)) //pops it from the queue, assigns it to destination, and destroys the original value.
        {
        val.show(); //To print/access the data of myarray for each popped Myclass object.
        }   
        }
    std::cout<< std::endl;
    return 0;
}

编译和执行可以按照此处所附的屏幕截图链接进行-->。 compilation and execution

希望对你有所帮助。

谢谢, 桑托什

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2011-06-18
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多