tbb::concurrent_bounded_queue 中使用的内部容器是什么?

问题描述

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

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

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

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

也欢迎任何其他建议。

解决方法

您可以将对象作为值存储在 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

希望对你有帮助。

谢谢, 桑托什

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...